CSI midterm
Which of the following will cause a syntax error?
(10 < num) && (num < 30) This one 10 < num < 20 10 < num && num < 25 num > 10 && num < 40
What is output of the following Java code? int num = 15; while (num > 0) num = num - 3; System.out.println(num);
0
String sentence; String str1, str2, str3, str4; int length1; sentence = " First exam is on Monday. " ; str1 = sentence.substring(6, 12); str2 = str1.substring(0, 4); str3 = sentence.replace( ' i ' , ' # ' ); str4 = sentence.indexOf( "on" ); length1 = sentence.length(); Based on the code above, what is the value of str4?
14
What is the output of the following Java code? int x = 57; int y = 3; switch (x % 9) { case 0: case 1: y++; case 2: y = y - 2; break; case 3: y = y + 2; case 4: break; case 5: case 6: y = y + 3; } System.out.println(y);
2, 5, 6, 57 ???
'A' < 'a' && 4 <= 8 || 2.5 >= 7 || 4 < y Based on the code above, which part of the expression is not evaluated?
2.5 >= 7 4 < y this one 2.5 >= 7 || 4 < y 4 <= 8
Suppose that you have the following code: int sum = 0; int num = 8; if (num < 0) sum = sum + num; else if (num > 5) sum = num + 15; After this code executes, what is the value of sum?
23
String sentence; String str1, str2, str3, str4; int length1; sentence = " First exam is on Monday. " ; str1 = sentence.substring(6, 12); str2 = str1.substring(0, 4); str3 = sentence.replace( ' i ' , ' # ' ); str4 = sentence.indexOf( "on" ); length1 = sentence.length(); What is the value of length1?
24
What is the value of counter after the following statements execute? counter = 1; while (counter < 30) counter = 2 * counter;
32
What is the output of the following statement? System.out.printf("%.2f", 48.9);
48.90
1, 1, 2, 3, 5, 8, 13, 21, ... What is the tenth Fibonacci number in the sequence above?
55
int x, y; if (x < 4) y = 2; else if (x > 4) { if (x > 7) y = 4; else y = 6; } else y = 8; based on the code above, what is the value of y if x = 5?
6
The expression (int) 8.7 evaluates to ____.
8
Relational Operators
==, !=, >, <, >=, <=
sentinel controlled loop
A loop that runs until a given condition becomes true.
EOF-controlled while loop
Continues to execute until the program detects the end-of-file marker
which of the following does not have an entry condition?
EOF-controlled while loop sentinel-controlled while loop this one???????? do...while loop for loop
What of the following does not have an entry condition?
EOF-controlled while loop This one? sentinel-controlled while loop do...while loop for loop
True or false: A "break statement is legal in a "while" loop, but not in a "for" loop.
False
True or false: A syntax error will result if the control statements of a for loop are omitted.
False
True or false: EOF-controlled while loop is another name for sentinel-controlled while loop.
False
True or false: Given a decima number, the method format of the class String returns the strong containing the digits of the formatted number.
False
True or false: Given a decimal number, the method format of the class String returns the string containing the digits of the formatted number.
False
True or false: In the for statement, if the logical expression is omitted, it is assumed to be false.
False
True or false: Java automatically initializes all variables.
False
True or false: Suppose console is a Scanner object initialized with the standard input device and feet and inches are int variables. Consider the following statements: feet = console.nextInt(); inches = console.nextInt(); These statements require the value of feet and inches to be input on separate lines.
False
True or false: Suppose console is a Scanner object initialized with the standard input device. The expression console.nextInt(); is used to read one int value and the expression console.nextDouble(); is used to read two int values.
False
True or false: Suppose z = 1834.762. The output of the statement System.out.printf("%.1f", z); is 1834.800.
False
True or false: The expression 'A' <= 'B' evaluates to false while the expression 'A' < 'B' evaulate to true
False
True or false: The operator = and == have the same order of precedence.
False
True or false: int alpha = 7; int beta = console.nextInt(); switch (beta) { case 5: alpha = alpha + 1; case 6: alpha = alpha + 2; case 7: alpha = alpha + 3; default: alpha = alpha + 5; } System.out.print(alpha); The output of this code is 9.
False
True or false: to use the content of the java.lang package in a program, they must be imported using the improt statement.
False
True or false: A computer program will recognize both = and == as the equality operator.
False
True or false: A while loop is a post-test loop.
False
True or false: The statement System.exit(0); is found at the end of every working Java program.
False
True or false: The following for loop executes 21 times. (Assume all variables are properly declared.) for (i = 1; i <= 20; i = i + 1) System.out.println(i);
False, 20 times
True or false: The output of the Java code, assuming that all variables are properly declared, is 32. num = 10; while (num <= 32) num = num + 5; System.out.println(num);
False, 35
True or false: The output of the Java code, assuming that all variables are properly declared, is: 23. num = 10; while (num <= 32) num = num + 5; System.out.print(num)
False, 35
True or false: if a = 4; and b = 3;, then after the statement a = b; executes, the value of b is 4 and the value of a is 3.
False, a and b = 3
True or false: In the class String , the substring method inserts a string into another string.
False, it selects specific parts in a string (1,3)
True or false: Suppose x = 8. After the execution of the statement y = x++; y is 8 and x is 10.
False, y = 9 x = 8
flag controlled loop
Loop where bool variable is used to control how many times the loop executes.
Given char ch; int num; double pay; Which of the following assignment statements are valid? (i) ch = ' * ' ; (ii) pay = num * pay; (iii) rate * 40 = pay;
Only (i) is valid this one (i) and (ii) are valid (ii) and (iii) are valid (i) and (iii) are valid
The standard output object in Java is ____.
System.out
Suppose that x is an int variable. Which of the following expressions always evaluates to false?
This one (x > 0) || (x <= 0) (x > 0) || (x == 0) (x > 0) && ( x <= 0) (x >= 0) && (x == 0)
Which of the following statements would store input in name?
This one name = JOptionPane.showInputDialog("Enter your name and press OK"); name = JOptionPane.showMessageDialog("Enter your name and press OK", JOptionPane.PLAIN_MESSAGE, null); name = System.in(); name = JOptionPane.input("Enter your name and press OK");
Which of the following statemnets would store input in name?
This one? name = JOptionPane.showInputDialog("Enter your name and press OK"); name = JOptionPane.showMessageDialog("Enter your name and press OK", JOptionPane.PLAIN_MESSAGE, null); name = System.in(); name = JOptionPane.input("Enter your name and press OK");
True or false: A counter-controlled loop is used when the exact numbeer of data entries is known
True
True or false: A loop is a control structure that causes certain statements to be executed over and over until certain conditions are met.
True
True or false: A program uses selection to implement a branch
True
True or false: After a break statement executes, the program continues to execute with the first statement after the structure.
True
True or false: Assume that all variables in the following code are properly declared and that the input is 3 7 4 -1. The output is 13. num = console.nextInt(); sum = num; while (num != -1) { num = console.nextInt(); sum = sum + num; } System.out.println(sum);
True
True or false: Both System.out.println and System.out.print can be used to output a string on the standard output device.
True
True or false: Control variables are automatically initialized in a loop.
True
True or false: If "length" denotes the length of a nonnull string, then "length - 1" give the index of the last character in the string.
True
True or false: In Java, !, &&, and || are called logic al operators.
True
True or false: In the case of an infinite while loop, the while expression (that is, the loop condition) is always true.
True
True or false: In the case of the sentinel-controlled while loop. the first item is read before the while loop is entered.
True
True or false: String variables are reference variables.
True
True or false: Suppose P and Q are logical expressions. The expression "P || Q" is true if both P and Q are true.
True
True or false: Suppose that index is an int variable. The statement index = index + 1; is equivalent to index++;
True
True or false: Suppose that name is a String variable. Then the following two statements are equivalent. name = "Danny"; name = new String("Danny");
True
True or false: Suppose x = 18.6. The output of the statement System.out.println((int)(x) / 3); is 6.
True
True or false: The control statements in the for loop include the initial expression, logical expression, and update expression.
True
True or false: The execution of a "break" statement in a "switch" statement terminates the "switch" statement
True
True or false: The expression "!(x <= 0)" is true only if x is a positive number
True
True or false: The loop condition of a "while" loop is reevaluated before every iteration of the loop.
True
True or false: The output of the following Java code is: Stoor. int count = 5; System.out.print("Sto"); do { System.out.print('o'); count--; } while (count >= 5); System.out.println('r');
True
True or false: The pair of characters // is used for single line comments.
True
True or false: The value of a variable may change during progrma execution.
True
True or false: A value such as 'd' is called a character constant.
True
True or false: The null string contains onlly the blank character.
True
True or false: the null string contains only the blank character.
True
True or false; Suppose that index is an int variable. The statement index= index + 1; is equivalent to index++
True
True or false: The class JOptionPane allows a programmer to use graphical interface components to obtain input from a user.
True, JOptionPane.showInputDialog
True or false: To read data from a file of unspecified length, an EOF-controlled while loop is a better choice than a counter-controlled while loop.
True, because EOF is a file reader
True or false: The classes Integer, Float, and Double are called wrapper classes.
True, wrapper classes also include boolean, byte, character, long, and short
If str1 is "Hello" and str2 is "Hi", which of the following could not be a result of str1.compareTo(str2);?
Which one? -9, -5, -1, 1
Which of the following is not a function of the break statement?
Which one? To exit early from a loop To skip the remainder of a switch structure To eliminate the use of certain boolean variables in a loop To ignore certain values for variables and continue with the next iteration of a loop
Which of the following loops is guaranteed to execute at least once?
Which one? counter-controlled while loop for loop This one do...while loop sentinel-controlled while loop
int i; for (i = 0; i <= 10; i++) System.out.println( " * " ); System.out.println( " ! " ); Which of the following is the update expression in the for loop above?
Which one? i = 0; i <= 10; this one? i++; System.out.println("*");
String sentence; String str1, str2, str3, str4; int length1; sentence = " First exam is on Monday. " ; str1 = sentence.substring(6, 12); str2 = str1.substring(0, 4); str3 = sentence.replace( ' i ' , ' # ' ); str4 = sentence.indexOf( "on" ); length1 = sentence.length(); Based on the code above, what is the value of str3?
Which one? "First exam is on Monday." This one? "F#rst exam #s on Monday." "F#rst exam is on Monday." "#i### #### i###########."
int x = 27; int y = 10; do x = x / 3; while (x >= y); If y = 0, how many times would the loop above execute?
Which one? 1 2 3 this one? 4
counter controlled loop
a loop whose processing is controlled by a counter; the loop body will be processed a precise number of times
Suppose that alpha and beta are int variables. The statement alpha = --beta; is equivalent ot the statement(s)___.
beta = beta - 1; alpha = beta;
True or false: Because a do...while loop is a post-test loop, the body of the loop may not execute at all.
false
True or false: In Java "| |" has a higher precedence than "&&"
false
True or false: The method printf is used only to format the output of integers and decimal numbers.
false
True or false: When one control statement is located within another, it is said to be a controlled statement.
false
True or false: the calss JOptionPane is part of the package java.swing.
false
boolean found = false; int num; int square; while (!found) { num = console.nextInt(); square = num * num; if (square > 40) found = true; } The above code is an example of a(n) ____ loop.
flag controlled loop
int sum = 0; int limit = console.nextInt(); int counter = 0; while (counter <= limit) { entry = console.nextInt(); sum = sum + entry; counter++; } System.out.println(sum); The above code is an example of a(n) ____ while loop.
flag-controlled This one counter-controlled EOF-controlled sentinel-controlled
What does >= mean?
greater than or equal to
Which of the following will cause a syntax error, if you are trying to compare x to 5?
if (x == 5) This one? if (x = 5) if (x <= 5) if (x >= 5)
Which of the following is a valid statement? (i) int num = new int( 67 ); (ii) String name = new ( " Doe " ); (iii) String name = " Doe " ;
iii
The declaration int a, b, c; is equivalent to which of the following?
int a , b c; this one int a; int b; int c; int abc; int a b c;
Which package is automatically imported by the Java system?
java.io this one java.lang java.util java.swing
In a_____ control structure, the computer executes particular statements depending on some condition(s).
looping, repetition, selection, sequence ???
Suppose str is a String variable. The statement str = new String("Programming"); is simmilar to which of the following?
new String = "Programming"; String new = "Programming"; this one str = "Programming"; str new "Programming";
switch (lastInitial) { case ' A ' : System.out.println( " section 1 " ); break; case ' B ' : System.out.println( " section 2 " ); break; case ' C ' : System.out.println( " section 3 " ); break; case ' D ' : System.out.println( " section 4 " ); break; default: System.out.println( " section 5 " ); } Based on the code above, what is the output if lastInitial = 'c'?
section 3
In a ____ control structure, the computer executes particular statements depending on some condition(s).
selection
static final int EndVal = -1; int double; int num = console.nextInt(); while (num != EndVal) { double = num * 2; System.out.println(double); num = console.nextInt(); } The above code is an example of a(n) ____ while loop.
sentinel-controlled
ch = inFile.next().charAt(); while (inFile.hasNext()) { System.out.println(ch); ch = inFile.next().charAt(); } The above code is an example of a(n) ____ loop.
sentinel-controlled flag-controlled this one? it's a file reader EOF-controlled counter-controlled
Suppose the String variable str points to the String object "Sunny day" at the memory address 3600. Then the value of str is:
the memory address 3600 "Sunny day" 3600 + "Sunny day" None of these
The conditional operator ?: takes _____ arguments.
two three four five
What is the output of the following Java code? int x = 0; if (x > 0) System.out.println("positive "); System.out.println("zero "); System.out.println("negative");
which one? zero negative zero negative positive zero negative
Where is the continue statement NOT usually used?
while loops for loops this one switch structures do...while loops