CIS225 Exam 2
The first fragment has a syntax error.
Analyze the following two code fragments: (i) int x = 5; if (0< x) && (x < 100) System.out.println("x is between 1 and 100"); (ii) int x = 5; if (0 < x && x < 100) System.out.println("x is between 1 and 100");
-2
"AbA".compareToIgnoreCase("abC") returns_________.
2
"abc".compareTo"aba") returns _________.
True
"unhappy".substring(2) returns "happy".
1
'3' - '2' + 'm' / 'n' is _________.
False
A break statement can be used only in a loop.
False
A break statement is required for a switch-case statement in Java.
'x' 120
An int variable can hold _____.
All three are correct, but Code 3 is preferred.
Analyze the following code fragments that assign a boolean value to the variable even. Code1: if (number % 2 == 0) even = true; else even - false; Code 2: even = (number % 2 == 0) ? true: false; Code 3: even = number % 2 == 0;
The code displays nothing.
Analyze the following code: boolean even = false; if (even) { System.out.println("It is even!"); }
The program compiles and runs fine.
Analyze the following code: double sum = 0; for (double d = 0; d < 10; sum += sum + d) { d += 0.1; }
The program compiles and runs, but it is not efficient and unnecessary to execute the Scanner input = new Scanner(System.in); statement inside the loop. You should move the statement before the loop
Analyze the following code: import java.util.Scanner; public class Test { public static void main(String[] args) { int sum = 0; for (int i = 0; i < 100000; i++) { Scanner input = new Scanner(System.in); sum += input.nextInt(); } } }
If number is zero, number is displayed.
Analyze the following code: // Enter an integer Scanner input = new Scanner(System.in); int number = input.nextInt(); if (number <= 0) System.out.println(number);
The symbol x is always printed.
Analyze the following code: int x = 0; if (x > 0); { System.out.println("x"); }
The switch control variable cannot be double.
Analyze the following fragment: double x = 0; double d = 1; switch (d +4) { case 5: x++; case 6: --x; }
The switch control variable cannot be double.
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; }
The program displays "It is even!"
Analyze: boolean even = ((231 % 2) == 0); if (even = true) System.out.println("It is even!"); else System.out.println("It is odd!");
i is an integer, but the format specifier %5.1f specifies a format for double value. The code has an error.
Analyze: int i = 3434; double d = 3434; System.out.printf("%5.1f%5.1f", i , d);
The code does not compile because the condition syntax is incorrect.
Analyze: int x = 1; while (0 < x) && (x < 100) System.out.println(x++);
number is printed out twice if number is negative number is printed out twice if number is zero number is always printed out at least once number is printed out once if number is positive
Analyze: //Enter an integer Scanner input = new Scanner(System.in); int number = input.nextInt(); if (number <=0 ) System.out.println(number); System.out.println(number);
ABac
Assume that the ASCII code for character c is 99 and for a is 97. What is the printout of the following code? System.out.println("AB" + 'a' + 'c');
ac
Assume that the ASCII code for character c is 99. What is the printout of the following code? System.out.print("a" + 'c');
x % 2 == 0 && y % 2 == 1 x % 2 == 0 || y % 2 == 0
Assume x = 14 and y = 15, which of the following is true?
x != 5
Assume x = 4, which of the following is true?
x equals 0
Assume x is 0. What is the output of the following statement? if (x > 0) printf("x is greater than 0"); else if (x < 0) printf("x is less than 0"); else printf("x equals 0");
!s1.contains(s2)
Given two String s1 = "Welcome to Java" and s2 = "Programming is fun", which of the following is true?
45
How many times is the println statement executed? for (int i = 0; i <10; i++) for (int j = 0; j < i; j++) System.out.println(i * j)
10
How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); count++; } while (count < 10);
true
In a for statement, if the continuation condition is blank, the condition is assumed to be _________.
False
In a switch statement, the default case must appear last among all cases. Otherwise, it would result in a compilation error.
a Boolean literal
In java, the word true is:
6.0
Math.ceil(5.5) evaluates to _________.
5.0
Math.floor(5.5) evaluates to ________.
27.0
Math.pow(3, 3) returns __________.
2.0
Math.sqrt(4) returns _________.
2.0
Math.sqrt(4) returns__________.
x.equals('a') x.equals(new Character('a'))
Suppose Character x = new Character('a'), _______ returns true.
in case cond1 is true and cond2 is true in case cond1 is false and cond2 is true in case cond1 is true and cond2 is false
Suppose cond1 and cond2 are two Boolean expressions. When will this if condition be true? if (cond1 || cond2)
char c = '5';
Which of the following assignment statements is correct to assign character 5 to c?
char c = 'd'; char c = 100;
Which of the following assignment statements is correct?
if (radius > 0) System.out.println(radius * radius * 3.14159);
Which of the following code displays the area of a circle if the radius is positive?
(ch >= 'A' && ch
To check whether a char variable ch is an uppercase letter, you write_________.
s1.concate(s2) + s3.concate(s4); s1.concate(s2).concate(s3).concate(s4); ((s1.concate(s2)).concate(s3)).concate(s4);
To concatenate s1, s2, s3, and s4, you write______.
Math.sin(Math.toRadians(35))
To obtain the sine of 35 degrees, use ________.
Double
parseDouble is a method in the ______ class.
^ && !
What are the Boolean operators?
Welcome12
What is "Welcome" + 1 + 1 * 2?
0
What is (int)Math.random()?
There is no guarantee that 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5 is true.
What is 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5?
There is no guarantee that 1.0 + 1.0 + 1.0 == 3.0 is true.
What is 1.0 + 1.0 + 1.0 == 3.0?
3.0
What is Math.floor(3.6)?
undefined
What is i after the following for loop? int y = 0; for (int i = 0; i<10; ++i) { y += i; }
n - 1
What is the number of iterations in the following loop? for (int i = 1; i < n; i++) { // iteration }
0 1 2 3
What is the possible output from System.out.println((int)Math.random() * 4)?
an empty string
What is the return value of "SELECT".substring(4, 4)?
1
What is the value of balance after the following code is executed?
1
What is the value of balance after the following code is executed? int balance = 10; while (balance >= 1) { if (balance < 9) break; balance = balance - 9; }
true
What is the value of the following expression? true || true && false
10
What is y after the following for loop statement is executed? int y = 0; for (int i = 0; i < 10; ++i) { y += 1; }
-10
What is y after the following statement is executed?
-1
What is y after the following switch statement? int x = 0; int y = 0; switch (x + 1) { case 0: y = 0; case 1: y = 1; default: y = -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 Test1 { public static void main(String[] args) { int x = 1; int y = x = x + 1; System.out.println("y is " + y); } }
Income is greater than 3000
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"); }
10
Suppose x = 10 and y = 10. What is x after evaluating the expression (y > 10) && (x++ > 10)?
0
Suppose x=0 and y=0. What is x after evaluating the expression (y > 0) && (1 > x++)?
10
Suppose x=10 and y=10. What is x after evaluating the expression (y >= 10) || (x -- > 10)?
99
The Unicode of 'a' is 97. What is the Unicode for 'c'?
System.exit(0);
The ________ method immediately terminates the program.
Double.parseDouble(s);
The _________ method parses a string s to a double variable.
Interger.parseInt(s);
The __________ method parses a string s to an int value.
True
The binary operator + is left-associative.
Java 123
The expression "Java" + 1 + 2 + 3 evaluates to _______.
-2
The expression 'c' - 'e' is ________.
1 3 5 7 9
The following loop displays ________. for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++; }
!=
The not equal comparison operator in Java is _____.
*, +, &, &&, ||
The order of the precedence (from high to low) of the operators binary +, *, &&, ||, & is __________.
True
The while loop and the do loop are equivalent in their expressive power; in other words, you can rewrite a while loop using a do loop, and vice versa.
if (s.startsWith("Java")) ... if (s.substring(0, 4).equals("Java")) ... if (s.charAt(0) == 'J' && if (s.charAt(1) == 'a' && if (s.charAt(2) == 'v' && if (s.charAt(3) == 'a') ... if (s.indexOf("Java") == 0) ...
To check if a string s contains the prefix "Java", you may write _______.
year % 4 == 0 && year % 100 !=0 || year % 400 == 0 (year % 4 == 0) && (year % 100 !-0) || (year % 400 == 0) year % 4 == 0 && (year % 100 !-0) || year % 400 == 0
Which of the following expression is equivalent to (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)?
toUpperCase() isDigit()
Which of the following is not a correct method in Character?
'4'
Which of the following is the correct expression of character 4?
"Java".toUpperCase()
Which of the following is the correct statement to return JAVA?
System.out.println("smith\\exam1\\test.txt");
Which of the following statement prints smith\exam1\test.txt?
s2.concates(s1); s2 += s1;
Which of the following statements are correct to concatenate string s1 into s2?
String s = 'r'; float f = 4.5; char c = "r"; int k = null;
Which of the following statements are incorrect?
No
Will System.out.println((char)4) display 4?
No
Will the following program terminate? int balance = 10; while (balance >= 1) { if (balance < 9) break; balance = balance - 9; }
true
You can always convert a for loop to a while loop.
True
You can always convert a switch statement to an equivalent if statement.
False
You can always convert an if statement to a switch statement.
False
You can cast a Boolean value to an int, or an int to Boolean.
s1.compareTo(s2)
You compare two strings s1 and s2 using __________.
"peter".equalsIgnoreCase("peter") "peter".equalsIgnoreCase("Peter") "peter".equals("peter")
______ returns true.
|| &&
_______ are short-circuit operators.