Docsity

Practice Quiz 2 with Solution Key - Programming I | COP 2210, Quizzes of Computer Science

Toggle sidebar
bg1
Quiz 2
Name: Panther ID:
Please use 15 minutes to finish this quiz.
1. Add the following lines to the main method:
int total = 10, count = 4.0;
float average = 6.67;
average = total / count;
System.out.println(average);
average = (float)total / count;
System.out.println(average);
average = total / (float)count;
System.out.println(average);
average = (float)(total / count);
System.out.println(average);
[2 pts] Is this program correct? If not, please correct it.
Not correct! The first two lines should be:
int total = 10, count = 4;
float average = 6.67f;
[3 pts] What is program output?
2.0
2.5
2.5
2.0
2. Add the following lines to the main method:
String s1 = new String(“\tWorld!\n”);
String s2 = “Hello”;
s3 = s1;
System.out.println(“s3: ” + s3);
s1 = s2 + s1;
System.out.println(“s3: ” + s3);
System.out.println(“Length of s1: ” + s1.length());
System.out.println(“Substring: ” + s1.substring(3, 6));
[1 pts] Is this program correct? If not, please correct it.
Not correct! Variable s3 should be declared as:
String s3 = s1;
[4 pts] What is program output?
s3: World!
s3: World!
Length of s1: 13
Substring: lo
pf2

Partial preview of the text

Download Practice Quiz 2 with Solution Key - Programming I | COP 2210 and more Quizzes Computer Science in PDF only on Docsity!

Quiz 2

Name: Panther ID:

Please use 15 minutes to finish this quiz.

1. Add the following lines to the main method:

int total = 10, count = 4.0; float average = 6.67; average = total / count; System.out.println(average); average = (float)total / count; System.out.println(average); average = total / (float)count; System.out.println(average); average = (float)(total / count); System.out.println(average);

[2 pts] Is this program correct? If not, please correct it.

Not correct! The first two lines should be: int total = 10, count = 4; float average = 6.67f;

[3 pts] What is program output?

2. Add the following lines to the main method:

String s1 = new String(“\tWorld!\n”); String s2 = “Hello”; s3 = s1; System.out.println(“s3: ” + s3); s1 = s2 + s1; System.out.println(“s3: ” + s3); System.out.println(“Length of s1: ” + s1.length()); System.out.println(“Substring: ” + s1.substring(3, 6));

[1 pts] Is this program correct? If not, please correct it.

Not correct! Variable s3 should be declared as: String s3 = s1;

[4 pts] What is program output?

s3: World!

s3: World!

Length of s1: 13

Substring: lo

3. Add the following lines to the main method:

import java.text.DecimalFormat; import java.text.NumberFormat; public class FormatDemo { public static void main(String []args) { //create DecimalFormat object DecimalFormat fmt = new DecimalFormat ("0.###"); System.out.println(fmt.format(5.9986)); System.out.println(fmt.format(3.8)); NumberFormat percentFmt = numberFormat.getPercentInstance(); System.out.print(percentFmt.format(0.06)); } }

[2 pts] Is this program correct? If not, please correct it.

Not correct! See above.

[3 pts] What is program output?