Computer Science midterm
For the following program, the program will print the word "Here" and then print: public class Question9 { public static void main(String[ ] args) { System.out.print("Here"); System.out.println("There " + "Everywhere"); System.out.println("But not" + "in Texas"); } }
"There Everywhere" on the same line as "Here"
Which of the following for loop headers will cause the body of the loop to be executed 100 times?
for(int i = 0; i < 100; i++)
Which of the following will yield a pseudorandom number in the range [ -5, +5 ) given the following: Random gen = new Random( );
gen.nextFloat( ) * 10 - 5
Which of the following will yield a pseudorandom number in the range [ -5, +5 ] given the following: Random gen = new Random( );
gen.nextInt( 11 ) - 5;
Which of the following expressions best represents the condition "if the grade is between 75 and 100"?
if (75 < grade && grade < 100)
Given that s is a String, what does the following loop do? for ( int j = s.length( ); j > 0; j-- ) System.out.print( s.charAt( j-1) );
it prints s out backwards
If the String s1 = "Software and hardware", what is returned by s1.charAt(1)?
o
What output is produced by the following code fragment? int limit =100, num1 = 15, num2 = 40; if ( limit <= 100) { if ( num1 == num2) System.out.print(" lemon "); System.out.print(" orange "); } System.out.println("grape ");
orange grape
Assume a, b and c are all floating point numbers. a=80.0, b=30.0, c=4.0. What is the result of x / y / z?
0.67
Assume x, y and z are all integers (int). x=80, y=30, and z=4. What is the result of x / y / z?
0.67
Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates? for ( int i=0; i<5; i++ ) x += i ;
10
How many iterations will the following for loop execute? for (int i = 1; i < 20; i = i + 2) { }
10
Given the following switch statement where x is 6, answer the questions below switch (x) { case 3 : x += 1; break; case 4 : x ++; break; case 5 : x --; break; case 6 : x *= 2; break; case 7 : x += 2; break; case 8 : x /= 2; break; case 9 : x ++; break; }
12
What is output with the statement System.out.println(x+y); if x and y are int values, where x=10 and y=5?
15
What value is contained in the integer variable size after the following statements are executed? size = 18; size = size + 12; size = size * 2; size = size / 4;
15
How many iterations will the following for loop execute? for (int i = 1; i <= 20; i++) { }
20
Given the nested if-else structure below: if (a > 0) { if (b < 0) x = x + 5; else { if (a > 5) x = x + 4; else x = x + 3; } } else x = x + 2; If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
3
Given the following switch statement where x is 6, answer the questions below switch (x) { case 3 : x += 1; case 4 : x ++; case 5 : x --; case 6 : x *= 2; case 7 : x += 2; case 8 : x /= 2; case 9 : x ++; }
8
What will be displayed by this command: System.out.println(Math.pow ((1+2), (2*3-4)));
9
The statement if (a >= b) a++; else b--; will do the same thing as the statement if (a < b) b--; else a++;
True
Suppose that String name = "Frank Zappa". What will the instruction name.toUpperCase( ).replace('A', 'I'); return?
B. "FRINK ZIPPI"
Which of the following would return the last character of the String s1?
C. s1.charAt( s1.length( )-1);
Which of the following lines is a properly formatted comment? a) // This is a comment b) /* This is a comment */ c) /* this is a comment */ D)All the above
D
We don't know x is positive or negative number. How to find the square root of the variable x?
Math.sqrt(Math.abs(x));
If the String s1 = "Hello World", which of the following would return the substring "Wor" from String s1?
D. s1.subString( 6, 9 );
A switch statement must have a default clause.
False
Each case in a switch statement MUST terminate with a break statement.
False
Every if statement requires an associated else statement, but not every else statement requires an associated if statement.
False
In Java, the symbol "=" and the symbol "==" are used synonymously (interchangeably).
False
In order to compare int, float and double variables, you can use <, >, ==, !=, <=, >=, but to compare char and String variables, you must use compareTo( ), equals( ) and equalsIgnoreCase( ).
False
Which of the following best describes this code snippet? if (count != 400) System.out.println("Hello World!");
If the variable count is not equal to 400, "Hello World" will be printed.
We would like to find the smallest integer which is larger than the current floating point number x, which method is best one?
Math.ceil(x);
What output will be generated by the following code? int code = 7; String day=""; switch( code ) { case 1: day="Monday"; break; case 2 : day="Tuesday"; break; case 3: day="Wednesday"; break; case 4 : day="Thursday"; break; case 5 : day="Friday"; break; } System.out.println(day);
No output
What output will be generated by the following code? String code = "2"; String day =""; switch( code.charAt(0) ) { case '1': day="Monday"; break; case '2' : day="Tuesday"; break; case '3': day="Wednesday"; break; case '4' : day="Thursday"; break; case '5' : day="Friday"; break; } System.out.println(day);
None
Which code statements that prompt for and read a double value from the user, and then print the result of raising that value to the third power?
Scanner scan = new Scanner(System.in); System.out.println("Enter a value: "); double num = scan.nextDouble(); System.out.println( (Math.pow(num, 3)) );
What is wrong, logically, with the following code? if (x > 10) System.out.println("Large"); else if (x > 6 && x <= 10) System.out.println("Medium"); else if (x > 3 && x <= 6) System.out.println("Small"); else System.out.println("Very small");
There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional
Assume that boolean done = false, int x = 10, int y = 12 The expression ( !done || x > y ) is true.
True
Assume that int y = 11, String s = "Goodbye". The expression ( (s.concat(t).length( ) < y) is true
True
Control in a switch statement jumps to the first matching case.
True
It is possible to convert any type of loop (while, do, or for) into any other.
True
The following for-loop is an infinite loop. for ( int j = 0; j < 1000; ) i++;
True
The initialization portion of a for loop header can be used to declare a variable that is used during loop execution.
True
What output will be generated by the following code? String code = "2"; String day=""; switch( code ) { case "1": day="Monday"; break; case "2" : day="Tuesday"; break; case "3": day="Wednesday"; break; case "4" : day="Thursday"; break; case "5" : day="Friday"; break; } System.out.println(day);
Tuesday
What output will be generated by the following code? char code = '2'; String day=""; switch( code ) { case '1': day="Monday"; break; case '2' : day="Tuesday"; break; case '3': day="Wednesday"; break; case '4' : day="Thursday"; break; case '5' : day="Friday"; break; } System.out.println(day);
Tuesday
Read code fragment and find the output number range: Random rand = new Random(); int num = rand.nextInt(26) - 10; System.out.println( num );
[-10, 15]
Read code fragment and find the output number range: Random rand = new Random(); float num2= rand.nextFloat()*2+1; System.out.println( num2 );
[1, 3)
A syntax error is a _____________________.
a compile-time error
In Java, a block statement is
a set of statements enclosed in { and }
What output is produced by the following code fragment? for (int value = 20; value >= 0; value -= 1 ) if ( value % 4 != 0) System.out.println( val );
print from 20 down to 0, except those that are evenly divisible by 4: 19, 18,17, 15, 14, 13, 11, 10, 9, 7, 6, 5, 3, 2, 1
The main method for a Java program is defined by
public static void main(String[] args)
What output is produced by the following code fragment given the assumptions below? if ( num1 < num2 ) System.out.print(" red "); if ( (num1 +5) < num2 ) System.out.print(" white "); else System.out.print(" blue "); System.out.println(" yellow ");
red blue yellow
Which of the following expressions correctly computes the value of the mathematical expression 5 + 26 ?
result = 5 + Math.pow(2, 6);
Rewrite the following nested if-else statement as an equivalent switch statement: if (letter == 'A' | | letter == 'a') System.out.println("Excellent"); else if (letter == 'B' | | letter == 'b') System.out.println("Very Good "); else if (letter == 'C' | | letter == 'c') System.out.println("Good"); else if (letter == 'D' | | letter == 'd') System.out.println("Do better"); else System.out.println("Try again! "); Which answer is correct?
switch ( letter ) { case 'A': case 'a' : System.out.println("Excellent"); break; case 'B': case 'b' : System.out.println("Very good"); break; case 'C': case 'c' : System.out.println("Good"); break; case 'D': case 'd' : System.out.println("Do better"); break; default : System.out.println("Try again"); }