CS120 Assignment 2 Total 100 points Print your name:______
Notes: Please use blue color to mark only one answer to the below questions. Due before 11 pm on Thursday
Please email your Assignment 2 to Dr. Zhang at
1. What will be displayed by the following Java code?
int numb1=6 ;
int numb2=2 ;
if (numb1< numb2)
{System.out.println(numb1/numb2);}
else
{System.out.println (numb2 * numb1 );}
A. 2 B. 4 C. 8 D. 12
2. What will be displayed by the following Java code?
A. 4 B. 6 C. 8 D. 12
3. What is the value of each variable (n, , k, r) after the if statement?
A. 3 4 3 B. 7 4 3 C. 5 3 2 D. 3 2 4
4. Which condition should be used to test whether the contents of two strings referenced by string1 and string2 have the same value?
A) (string1.equals(string2))
B) (String.equals(string1, string2))
C) (string1 = string2)
D) (Equals(string1, string2))
5. What is the value of each variable (n, , k, r) after the if statement?
A. 1 2 3 B. 2 3 1 C. 5 2 3 D. 3 2 1
6. What type of statement can be used to implement a sequence of if/else/else that compares a single value against several constant alternatives (We did in Lab 6.)?
A) compare
B) ifElse
C) switch
D) sequence
7. Which code fragment involving a switch correctly maps a letter grade to a gpa value, assuming the following declarations?
String letterGrade;
double gpaValue;
A) Char letter = letterGrade.charAt(0);
switch (letter)
{
'A':
gpaValue = 4.0;
break;
...
}
B) switch (letterGrade)
{
"A":
gpaValue = 4.0;
...
}
C) switch (letterGrade)
{
"A": gpaValue = 4.0; break;
...
}
D) switch (letterGrade)
{
A:
gpaValue = 4.0;
break;
...
}
8. Consider the following code fragment:
String letterGrade = "";
if (grade < 90){
if (grade >= 80)
letterGrade = "B";
else
letterGrade = "A";
}
System.out.println(letterGrade);
What is the value of letterGrade when the grade is 95?
A) null
B) "A"
C) "B"
D) ""
9. What will be displayed by the following Java code fragments when the command button is clicked?
int a=9; int b=8 ; int c=6;
if ( (a > c) & (b < c) ) {
System.out.println( a); }
else {
if ( (b < c) || (c < a) )
{ System.out.println( b); }
else
{ System.out.println( c); }
}
(A)9 ( B) 6 (C) 8 (D) none of correct
10. What will be displayed on the screen by running the below partial Java program?
int x=9; int y=400;
if (x >=20 & y <= 200) {
System.out.println( "Jeff"); }
else if (x > y){
System.out.println(" Robert"); }
else if (y >x & x <18) {
System.out.println("Nancy");}
else if (x < 7 || y >300) {
System.out.println("Muller");}
else
{ System.out.println("Murk");}
(A) Jeff (B) Robert ( C) Nancy (D) Muller (E) Murk
11. What will be X value of the following Java code fragments?
int num=6; int X;
if (num <= 3)
{ X = 2 * num; }
else if ( num > 7 )
{ X = 3 * num;}
else
{X = 4 * num; }
System.out.println(X);
(A) 12 (B) 18 (C) 0 (D) 24
12. What will be displayed on the screen of the following Java code fragments?
int x=20; int y=2; int z=4;
if ( ((x > y) || (y > z) ) & (z >= 22) )
{ System.out.println( "Hello"); }
else
{
if ( (x < y) || (y < z ) || ( z <=3) )
{ System.out.println( "Good-bye" ); }
else
{ System.out.println( "Larry" );}
}
(A)Hello (B)Good-bye (C) Hello Larry (D)Larry
13. What will be displayed by the following Java code fragments when the command button is clicked?
String letterGrade=null;
int grade = 75;
if (grade >= 90 )
{letterGrade = "A";}
else if ( grade >= 80 & grade < 90)
{letterGrade = "B";}
else if( grade >= 70 & grade < 80 )
{letterGrade = "C";}
else if ( grade >= 60 & grade < 70)
{letterGrade = "D";}
else
{letterGrade = "E";}
System.out.println(letterGrade);
a. A b. B c. C d. D
14. In Java, what reserved word is used to identify constants?
A) final
B) double
C) constant
D) int
15. Which of the following correctly defines a constant in a method?
A) public static final double LITERS_PER_GALLON == 3.785
B) final double NICKEL_VALUE == 0.05;
C) final double NICKEL_VALUE = 0.05;
D) public static final double LITERS_PER_GALLON = 3.785;
16. Which of the following correctly defines a constant in a class?
A) public static final int HOURS_PER_DAY == 24;
B) final int HOURS_PER_DAY = 24;
C) final int DAYS_PER_YEAR == 365;
D) public static final int DAYS_PER_YEAR = 365;
17. What is the Java operator that computes the remainder of an integer division?
A) %
B) mod
C) /
D) div
18. Which statement correctly computes the square root of the given number?
A) int s = Math.sqrt(16);
B) int s = Math.squareroot(16);
C) double s = Math.sqrt(16);
D) double s = Math.squareroot(16);
19. Which of the following statements is equivalent to balance = balance + amount; ?
A) balance += amount;
B) balance == amount;
C) balance +== amount;
D) balance =+ amount;
20. Which of the following statements is equivalent to items = items * 2; ?
A) items *= 2;
B) items *== 2;
C) items =* 2;
D) items ==* 2 ;
21. Convert the following string to its integer value as quantity:
String input = "6";
A) int quantity = Integer.parseInt(input);
B) double quantity = Double.parseDouble(input);
C) int quantity = String.parseString(input);
D) int quantity = Integer.parseString(input);
22. Convert the following string to its floating-point value as price:
String input = "75.23";
A) double price = Double.parseString(input);
B) double price = Double.parseDouble(input);
C) int price = Integer.parseInt(input);
D) double price = String.parseString(input);
23. Based on the code below, which statement extracts the string "Hello"?
String greeting = "Hello, World!";
A) String hello = greeting.substring(",");
B) String hello = greeting.substring(5);
C) String hello = greeting.substring(1,6);
D) String hello = greeting.substring(0,5);
24. Based on the code below, which statement creates a string in the format "Hello, NAME!"?
String hello = "Hello";
String name; // Assume value is initialized by the program
A) String helloName = hello + name + "!";
B) String helloName = hello + ", " + name + "!";
C) String helloName = hello + ", " + name;
D) String helloName = hello + name;
25. Based on the code below, write a statement that copies all characters after the comma to the string sub.
String greeting = "Hello, World!";
A) String sub = greeting.substring(" ");
B) String sub = greeting.substring(7);
C) String sub = greeting.substring(6);
D) String sub = greeting.substring(",");