Java Final
Which of the following are valid specifiers for the printf statement? (Choose all that apply.) 11) A) %10b B) %8.2d C) %4c D) %10.2e E) %6d
%10b,%4c,%10.2e %6d
Which of the following is the best for generating random integer 0 or 1? 33) A) (int)Math.random() B) (int)(Math.random() + 0.2) C) (int)(Math.random() + 0.8) D) (int)Math.random() + 1 E) (int)(Math.random() + 0.5)
(int)(Math.random() + 0.5)
The statement System.out.printf("%10s", 123456) outputs ________. (Note: * represents a space) A) ****123456 B) 12345***** C) 123456**** D) 23456***** Think of spacing before the problem
****123456
The statement System.out.printf("%10s", 123456) outputs ________. (Note: * represents a space) 9) A) 12345***** B) 23456***** C) 123456**** D) ****123456
****123456
The order of the precedence (from high to low) of the operators +, *, &&, ||, & is: A) *, +, &, &&, || B) *, +, &&, ||, & C) &&, ||, &, *, + D) &, ||, &&, *, + E) *, +, &, ||, &&
*, +, &, &&, ||
What is y after the following statement is executed? x = 0; y = (x > 0) ? 10 : -10; A) 0 B) 20 C) -10 D) 10 E) Illegal expression
-10
Suppose x is 1. What is x after x -= 1? A) 0 B) 1 C) 2 D) -1 E) -2
0
How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (++count < 10); 14) A) 11 B) 8 C) 0 D) 9 E) 10
10
How many times will the following code print "Welcome to Java"? int count = 0; while (count < 10) { System.out.println("Welcome to Java"); count++; } A) 11 B) 8 C) 0 D) 10 E) 9
10
How many times will the following code print "Welcome to Java"? int count = 0; while (count++ < 10) { System.out.println("Welcome to Java"); } A) 0 B) 10 C) 9 D) 11 E) 8
10
How many times will the following code print "Welcome to Java"? int count = 0; while (count++ < 10) { System.out.println("Welcome to Java"); } 17) A) 9 B) 10 C) 8 D) 11 E) 0
10
Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x-- > 10)? A) 11 B) 9 C) 10
10
Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x-- > 10)? A) 11 B) 9 C) 10
10
What is the value in count after the following loop is executed? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 9); System.out.println(count); A) 8 B) 0 C) 9 D) 10 E) 11
10
How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 10); A) 11 B) 0 C) 8 D) 9 E) 10
11
How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 10); A) 8 B) 11 C) 10 D) 0 E) 9
11
What is the result of 45 / 4? A) 11 B) 10 C) 11.25 D) 12
11
An int variable can hold ________. (Choose all that apply.) A) 120 B) 120.0 C) "x" D) "120" E) 'x'
120, 'x'
The statement System.out.printf("%5d", 123456) outputs ________. 12) A) 23456 B) 123456 C) 12345.6 D) 12345
123456
What is the output of the following program? import java.util.Date; public class Test { public static void main(String[ ] args) { Date date = new Date(1234567); m1(date); System.out.print(date.getTime() + " "); m2(date); System.out.println(date.getTime()); } public static void m1(Date date) { date = new Date(7654321); } public static void m2(Date date) { date.setTime(7654321); } } A) 7654321 1234567 B) 1234567 7654321 C) 1234567 1234567 D) 7654321 7654321
1234567 7654321
Given the following method static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); n--; } } What is k after invoking nPrint("A message", k)? int k = 2; nPrint("A message", k); A) 2 B) 0 C) 1 D) 3
2
Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x++ > 10)? 13) A) 10 B) 9 C) 11
23456
What is the printout of System.out.println('z' - 'a')? A) z B) a C) 25 D) 26
25
What is Math.ceil(3.6)? A) 3 B) 3.0 C) 4.0 D) 5.0
4.0
What is Math.rint(3.5)? A) 3.0 B) 5.0 C) 4.0 D) 3 E) 4
4.0
What is Math.rint(3.6)? A) 4.0 B) 3.0 C) 5.0 D) 3
4.0
How many times are the following loops executed? for (int i = 0; i < 10; i++) for (int j = 0; j < i; j++) System.out.println(i * j) A) 100 B) 10 C) 20 D) 45
45
What is sum after the following loop terminates? int sum = 0; int item = 0; do { item++; sum += item; if (sum > 4) break; } while (item < 5); A) 5 B) 7 C) 6 D) 8 Hint Think about the Item++ maybe its a subtraction problem in the end
6
What is sum after the following loop terminates? int sum = 0; int item = 0; do { item++; sum += item; if (sum > 4) break; } while (item < 5); 15) A) 6 B) 8 C) 7 D) 5
6
Which of the following operators are right-associative? A) + B) % C) * D) = E) &&
=
Which of the following statement is most accurate? (Choose all that apply.) A) An object may contain other objects. B) A reference variable refers to an object. C) An object may contain the references of other objects. D) A reference variable is an object.
A reference variable refers to an object. AND An object may contain the references of other objects.
Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if (number % 2 == 0) even = true; else even = false; Code 2: even = (number % 2 == 0) ? true: false; Code 3: even = number % 2 == 0; A) Code 3 has a compile error, because you attempt to assign number to even. B) All three are correct, but Code 2 is preferred. C) All three are correct, but Code 1 is preferred. D) Code 2 has a compile error, because you cannot have true and false literals in the conditional expression. E) All three are correct, but Code 3 is preferred.
All three are correct, but Code 3 is preferred.
________ is the code with natural language mixed with Java code. A) A flowchart diagram B) Java program C) Pseudocode D) A Java statement Hint _____-science
Pseudocode
In Java, the word true is ________. A) same as value 1 B) same as value 0 C) a Boolean literal D) a Java keyword
a Boolean literal
Which of these data types requires the most amount of memory? A) byte B) int C) long D) short
long
What is the number of iterations in the following loop: for (int i = 1; i <= n; i++) { // iteration } A) n B) n + 1 C) n - 1 D) 2*n
n
What is the number of iterations in the following loop: for (int i = 1; i < n; i++) { // iteration } A) 2*n B) n C) n + 1 D) n - 1 Hint if n is greater than 1 then it should be subtraction
n - 1
________ is to implement one method in the structure chart at a time from the top to the bottom. A) Bottom-up and top-down approach B) Top-down approach C) Stepwise refinement D) Bottom-up approach
Top-down approach
Does the following statement smith\exam1\test.txt? Print this System.out.println("smith\\exam1\\test.txt"); True Or False?
True
Analyze the following code. boolean even = false; if (even) { System.out.println("It is even!"); } 21) A) The code is wrong. You should replace if (even) with if (even = true) B) The code displays nothing. C) The code is wrong. You should replace if (even) with if (even == true) D) The code displays It is even!
The code displays nothing.
Analyze the following code. public class Test { public static void main(String[ ] args) { System.out.println(m(2)); } public static int m(int num) { return num; } public static void m(int num) { System.out.println(num); } } A) The program runs and prints 2 once. B) The program runs and prints 2 twice. C) The program has a compile error because the two methods m have the same signature. D) The program has a compile error because the second m method is defined, but not invoked in the main method.
The program has a compile error because the two methods m have the same signature.
Analyze the following fragment: double sum = 0; double d = 0; while (d != 10.0) { d += 0.1; sum += sum + d; } A) The program does not compile because sum and d are declared double, but assigned with integer value 0. B) The program never stops because d is always 0.1 inside the loop. C) After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9 D) The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
Analyze the following fragment: double sum = 0; double d = 0; while (d != 10.0) { d += 0.1; sum += sum + d; } A) The program never stops because d is always 0.1 inside the loop. B) After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9 C) The program does not compile because sum and d are declared double, but assigned with integer value 0. D) The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
Analyze the following code: boolean even = false; if (even = true) { System.out.println("It is even!"); } A) The program runs fine, but displays nothing. B) The program runs fine and displays It is even!. C) The program has a runtime error. D) The program has a compile error. think about what is being printed*
The program runs fine and displays It is even!.
Analyze the following code: if (x < 100) && (x > 10) System.out.println("x is between 10 and 100"); A) The statement compiles fine. B) The statement has compile errors because (x<100) & (x > must be enclosed inside parentheses and the println(...) statement must be put inside a block. C) The statement compiles fine, but has a runtime error. D) The statement has compile errors because (x<100) & (x > 1 must be enclosed inside parentheses.
The statement has compile errors because (x<100) & (x > 1 must be enclosed inside parentheses.
Analyze the following program fragment: int x; double d = 1.5; switch (d) { case 1.0: x = 1; case 1.5: x = 2; case 2.0: x = 3; } 1) A) The switch control variable cannot be double. B) The program has a compile error because the required break statement is missing in the switch statement. C) The program has a compile error because the required default case is missing in the switch statement. D) No errors.
The switch control variable cannot be double.
What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0? A) true B) false C) There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true.
There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true.
Which of the following should be declared as a void method? A) Write a method that checks whether current second is an integer from 1 to 100. B) Write a method that prints integers from 1 to 100. C) Write a method that converts an uppercase letter to lowercase. D) Write a method that returns a random integer from 1 to 100.
Write a method that prints integers from 1 to 100.
Do the following two statements in (I) and (II) result in the same value in sum? (I): for (int i = 0; i<10; ++i) { sum += i; } (II): for (int i = 0; i<10; i++) { sum += i; } A) YesB) No
Yes
What is the printout of the following switch statement? char ch = 'a'; switch (ch) { case 'a': case 'A': System.out.print(ch); break; case 'b': case 'B': System.out.print(ch); break; case 'c': case 'C': System.out.print(ch); break; case 'd': case 'D': System.out.print(ch); } A) abc B) abcd C) ab D) aa E) a Hint look at what ch really is
a
To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy? A) add 1.00, 0.99, 0.98, ..., 0.02, 0.01 in this order to a sum variable whose initial value is 0. B) add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.
add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.
What is the printout of the following switch statement? char ch = 'b'; switch (ch) { case 'a': System.out.print(ch); case 'b': System.out.print(ch); case 'c': System.out.print(ch); case 'd': System.out.print(ch); } A) bcd B) bbb C) bb D) abcd E) b
bbb
What is the printout of the following switch statement? char ch = 'b'; switch (ch) { case 'a': System.out.print(ch); case 'b': System.out.print(ch); case 'c': System.out.print(ch); case 'd': System.out.print(ch); } 10) A) b B) bb C) bcd D) abcd E) bbb
bbb
An object is an instance of a ________. A) data B) class C) method D) program
class
Variables that are shared by every instances of a class are ________. A) private variables B) class variables C) instance variables D) public variables
class variables
According to Java naming convention, which of the following names can be variables? (Choose all that apply.) 5) A) findArea B) TOTAL_LENGTH C) FindArea D) totalLength E) class 1
findArea, totalLength,
Which of the following code displays the area of a circle if the radius is positive? A) if (radius >= 0) System.out.println(radius * radius * 3.14159); B) if (radius != 0) System.out.println(radius * radius * 3.14159); C) if (radius > 0) System.out.println(radius * radius * 3.14159); D) if (radius <= 0) System.out.println(radius * radius * 3.14159);
if (radius > 0) System.out.println(radius * radius * 3.14159);
To declare an int variable number with initial value 2, you write 18) A) int number = 2.0; B) int number = 2L; C) int number = 2l; D) int number = 2;
int number = 2
The following code displays ________. double temperature = 50; if (temperature >= 100) System.out.println("too hot"); else if (temperature <= 40) System.out.println("too cold"); else System.out.println("just right"); A) too cold B) too hot C) too hot too cold just right D) just right Hint Think about what the last print says
just right
What code may be filled in the blank without causing syntax or runtime errors: public class Test { java.util.Date date; public static void main(String[ ] args) { Test test = new Test(); System.out.println(________); } } A) test.date B) date.toString() C) test.date.toString() D) date
test.date
What is i after the following for loop? int y = 0; for (int i = 0; i<10; ++i) { y += i; } A) 9 B) 11 C) 10 D) undefined
undefined
How many JFrame objects can you create and how many can you display? A) three B) one C) two D) unlimited
unlimited
What is y displayed? public class Test { public static void main(String[ ] args) { int x = 1; int y = x + x++; System.out.println("y is " + y); } } 19) A) y is 3. B) y is 4. C) y is 2. D) y is 1.
y is 2
What is y displayed in the following code? public class Test1 { public static void main(String[ ] args) { int x = 1; int y = x = x + 1; System.out.println("y is " + y); } } 6) A) y is 2 because x + 1 is assigned to x and then x is assigned to y. B) y is 1 because x is assigned to y first. C) y is 0. D) The program has a compile error since x is redeclared in the statement int y = x = x + 1.
y is 2 because x + 1 is assigned to x and then x is assigned to y.
What is y displayed in the following code? public class Test { public static void main(String[ ] args) { int x = 1; int y = x++ + x; System.out.println("y is " + y); } } A) y is 1. B) y is 4. C) y is 2. D) y is 3.
y is 3.