CSC 2040 Java Programming Quiz 2
Which of the following are so called short-circuit operators? Question 9 options: A) && B) & C) || D) |
A) && C) ||
Suppose s1 and s2 are two strings. Which of the following statements or expressions is incorrect? A) String s3 = s1 - s2; B) boolean b = s1.compareTo(s2); C) char c = s1[0]; D) char c = s1.charAt(s1.length());
A) String s3 = s1 - s2; B) boolean b = s1.compareTo(s2); C) char c = s1[0]; D) char c = s1.charAt(s1.length());
Analyze the following code: boolean even = false; if (even = true) { System.out.printIn("It is even"); } A) The program runs fine and displays It is even. B) The program runs fine, but displays nothing. C) The program has a runtime error. D) The program has a compile error.
A) The program runs fine and displays It is even.
Suppose s1 and s2 are two strings. What is the result of the following code?s1.equals(s2) == s2.equals(s1) A) True B) False
A) True
In Java, the word true is ________. A) a Boolean literal B) a Java keyword C) same as value 0 D) same as value 1
A) a Boolean literal
What is 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5? Question 4 options: A) True B) False C) There is no guarantee that 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5 is true.
A) true
What is the value of the following expression?true || true && false Question 3 options: A) true B) false
A) true
Suppose Character x = new Character('a'), __________________ returns true. A) x.equals(new Character('a')) B) x.compareToIgnoreCase('A') C) x.equalsIgnoreCase('A') D) x.equals('a') E) x.equals("a")
A) x.equals(new Character('a')) D) x.equals('a')
Which of the following assignment statements is correct? A. char c = 'd'; B. char c = 100; C. char c = "d"; D. char c = "100";
A. char c = 'd'; B. char c = 100;
Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative? A) 1 < x < 100 && x < 0 B) ((x < 100) && (x > 1)) || (x < 0) C) ((x < 100) && (x > 1)) && (x < 0) D) (1 > x > 100) || (x < 0)
B) ((x < 100) && (x > 1)) || (x < 0)
Math.sin(Math.PI) returns ________. A) 1.0 B) 0.0 C) 0.4 D) 0.5
B) 0.0
The statement System.out.printf("%10s", 123456) outputs ________. (Note: * represents a space.) A) 12345***** B) 23456***** C) 123456**** D) ****123456
B) 23456*****
Suppose income is 4001, what is the output of the following code? if (income > 3000) { System.out.println("Income is greater than 3000"); } else if (income > 4000) { System.out.println("Income is greater than 4000"); } A) no output B) Income is greater than 3000 C) Income is greater than 4000 followed by Income is greater than 3000 D) Income is greater than 4000
B) Income is greater than 3000
A Java character is stored in ________. A) one byte B) two bytes C) three bytes D) four bytes
B) two bytes
Assume x = 4 and y = 5. Which of the following is true? Question 10 options: A) x == 5 ^ y == 4 B) x != 4 ^ y == 5 C) !(x == 4) ^ y != 5 D) x != 5 ^ y != 4
B) x != 4 ^ y == 5
Assume x = 4 and y = 5. Which of the following is true? A) x < 5 && y < 5 B) x < 5 || y < 5 C) x > 5 && y > 5 D) x > 5 || y > 5
B) x < 5 || y < 5
What is the output of the following code? int x = 0; if (x < 4) { x = x+1; } System.out.println("x is" + x); A) x is 4 B) x is 1 C) x is 0 D) x is 2
B) x is 1
Which of the Boolean expressions below is incorrect? Question 1 options: A) (x > 0) || (x < 0) B) !(x > 0) && (x > 0) C) (x != 0) || (x = 0) D) (true) && (3 => 4) E) (-10 < x < 0)
B, D, E B) !(x > 0) && (x > 0) D) (true) && (3 => 4) E) (-10 < x < 0)
'3' - '2' + 'm' / 'n' is ______. A. 0 B. 1 C. 2 D. 3
B. 1
"abc".compareTo("aba") returns ________. A) -2 B) 2 C) -1 D) 1 E) 0
B. 2
Will System.out.println((char)4) display 4? A. Yes B. No
B. No
____________________ returns true. A) "peter".compareToIgnoreCase("Peter") B) "peter".compareToIgnoreCase("peter") C) "peter".equalsIgnoreCase("Peter") D) "peter".equalsIgnoreCase("peter") E) "peter".equals("peter")
C) "peter".equalsIgnoreCase("Peter") D) "peter".equalsIgnoreCase("peter") E) "peter".equals("peter")
Which of the following is the correct expression of character 4? A) 4 B) "4" C) '4' D) '\0004'
C) '4'
What is Math.ceil(3.6)? A) 3.0 B) 3 C) 4.0 D) 5.0
C) 4.0
Suppose isPrime is a boolean variable. Which of the following is the correct and best statement for testing if isPrime is true? A) if (isPrime = true) B) if (isPrime == true) C) if (isPrime) D) if (!isPrime = false) E) if (!isPrime == false)
C) if (isPrime)
To check if a string s contains the suffix "Java", you may write A) if (s.substring(s.length() - 5).equals("Java")) ... B) if (s.lastIndexOf("Java") >= 0) ... C) if (s.endsWith("Java")) ... D) if (s.substring(s.length() - 4).equals("Java")) ... E) if (s.charAt(s.length() - 4) == 'J' && s.charAt(s.length() - 3) == 'a' && s.charAt(s.length() - 2) == 'v' && s.charAt(s.length() - 1) == 'a') ...
C) if (s.endsWith("Java")) ... D) if (s.substring(s.length() - 4).equals("Java")) ... E) if (s.charAt(s.length() - 4) == 'J' && s.charAt(s.length() - 3) == 'a' && s.charAt(s.length() - 2) == 'v' && s.charAt(s.length() - 1) == 'a') ...
Which of the following is a possible output from invoking Math.random()? A) 3.43 B) 1.0 C) 0.0 D) 0.5
C)0.0 D) 0.5
What is Math.rint(3.6)? A) 3.0 B) 3 C) 4.0 D) 5.0
C. 4.0 Explanation: Note that rint returns a double value
The expression "Java " + 1 + 2 + 3 evaluates to ________. A) java 123 B) Illegal expression C) Java 123 D) Java123 E) Java6
C. Java 123
Analyze the following code: int i = 3434; double d = 3434; System.out.printf("%5.1f %5.1f", i, d); A. The code compiles and runs fine to display 3434.0 3434.0. B. The code compiles and runs fine to display 3434 3434.0. C. i is an integer, but the format specifier %5.1f specifies a format for double value. The code has an error.
C. i is an integer, but the format specifier %5.1f specifies a format for double value. The code has an error.
Given |x - 2| <= 4, which of the following is true? A) x - 2 <= 4 || x - 2 >= -4 B) x - 2 <= 4 && x - 2 >= 4 C) x - 2 <= 4 && x - 2 >= -4 D) x - 2 <= 4 && x - 2 > -4
C. x - 2 <= 4 && x - 2 >= -4
What is y displayed in the following code? public class Test1 { public static void main(String[] args) {i nt x = 1; int y = x = x + 1; System.out.println("y is " + y);} } A. y is 0. B. y is 1 because x is assigned to y first. C. y is 2 because x + 1 is assigned to x and then x is assigned to y. D. The program has a compile error since x is redeclared in the statement int y = x = x + 1.
C. y is 2 because x + 1 is assigned to x and then x is assigned to y.
To obtain the sine of 35 degrees, use ________. A) Math.sin(Math.toDegree(35)) B) Math.sin(Math.toDegrees(35)) C) Math.sin(Math.toRadian(35)) D) Math.sin(Math.toRadians(35)) E) Math.sin(35)
D) Math.sin(Math.toRadians(35))
What is the return value of "SELECT".substring(4, 4)? A) C B) T C) E D) an empty string
D) an empty string
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);
D) if (radius > 0) System.out.println(radius * radius * 3.14159);
The statement System.out.printf("%3.1e", 1234.56) outputs ________. A) 0.123456e+04 B) 1.23+03 C) 0.123e+04 D) 1.2e+03 E) 0.1e+04
D. 1.2e+03
Analyze the following code: Code 1: int number = 45; bool even; if (number % 2 == 0) even = true; else even = false; Code 2: int number = 45; bool even = (number % 2 == 0); A. Code 1 has compile errors. B. Code 2 has compile errors. C. Both Code 1 and Code 2 have compile errors. D. Both Code 1 and Code 2 are correct, but Code 2 is better.
D. Both Code 1 and Code 2 are correct, but Code 2 is better.
Which of the following operators is right-associative. A) && B) * C) % D) + (binary +) E) =
E)=
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 2 has a compile error, because you cannot have true and false literals in the conditional expression. B. Code 3 has a compile error, because you attempt to assign number to even. C. All three are correct, but Code 1 is preferred. D. All three are correct, but Code 2 is preferred. E. All three are correct, but Code 3 is preferred.
E. All three are correct, but Code 3 is preferred.