cs exercise 3
For a String str, what are the indexes of the first and last characters? 0, str.length() - 1 1, str.length() - 1 0, str.length() 1, str.length()
0, str.length() - 1
How may stars will be printed after the execution of the following code segment? for (int i = 0; i < 10; i++); System.out.println("*"); 1 infinite loop compile error 10
1
How may stars will be printed by the following code segment? for(int i = 0; i < 10; i++) System.out.println("*"); 9 10 compile error infinite loop
10
What will be the output from the following code segment? int num = 0; while (num < 10) { num = num + 1;} System.out.println(num); cannot determine 11 9 10
10
What will be printed after the execution of the following code segment: int sum = 0; for(int i = 0; i < 10; i = i + 2) { sum = sum + i;} System.out.print(sum); 20 10 30 45
20
What's the output of the following code segment if the following is used as the input from the keyboard? 5 3 6 5 7 8 2 1 9 0 public static void main(String[] args) { Scanner stdin = new Scanner(System.in); double target = stdin.nextDouble(); int cnt = 0; double val = 1; while (val > 0 && cnt < 3) { val = stdin.nextDouble(); if (val > target) { cnt++; } } System.out.print(cnt + " " + val); stdin.close(); } 3 9.0 3 8.0 2 0.0 3 0.0
3 8.0
Which of the following statements are true about the following code segment? int sum = 0; for(int i = 0; i < 10;) { sum = sum + i; i++;} System.out.println(sum); The program has a compile error. 55 will be printed. 45 will be printed. The program will end in an infinite loop.
45 will be printed.
How many lines of stars will be printed by the following code segment? int num = 0;while (num++ < 5) { System.out.println("*");} 7 4 6 5
5
What's the output of the following code? int num = 17; int cnt = 0; while(num != 0) { num = num / 2; cnt++;} System.out.print(cnt); There is an infinite loop. 6 5 4
5
Given n = 5, what's the value of n after executing the following code snippet? if(n++ > 5) n++; 5 6 7
6
Given n = 5, what's the value of n after executing the following code snippet? if(n++ >= 5) n++; 5 7 6
7
How many times will the following program print "Hello World"? int count = 0; while (++count < 10) { System.out.println("Hello World"); } 9 11 12 10
9
Which of the following counts the number of characters 'a' and 'A' in str correctly? Check all that apply. A. int cnt = 0; for (int i = 0; i < str.length; i++) { if (str.charAt(i) == 'a' || str.charAt(i) == 'A') cnt++; } B. int cnt = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == 'a' || str.charAt(i) == 'A') cnt++; } C. int cnt; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == 'a' || str.charAt(i) == 'A') cnt++; } D. int cnt = 0; for (int i = str.length() - 1; i >= 0; i--) { if (str.charAt(i) == 'a' || str.charAt(i) == 'A') cnt++; }
B. int cnt = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == 'a' || str.charAt(i) == 'A') cnt++; } D. int cnt = 0; for (int i = str.length() - 1; i >= 0; i--) { if (str.charAt(i) == 'a' || str.charAt(i) == 'A') cnt++; }
Given the following input Coding is fun Java is powerful Practice is important What is the value ofstr2after executing the following statements? str1 = stdin.nextLine(); str2 = stdin.nextLine(); str3 = stdin.nextLine(); A blank space. Java is powerful Practice is important is
Java is powerful
For the following code segment int sum = 0; for(int i = 0; i < 5;) { sum = sum + i; } System.out.println(sum); The program has a compile error. 15 will be printed. The program will end in an infinite loop. 10 will be printed.
The program will end in an infinite loop.
true, false are ___________ in Java. keywords identifiers boolean literals none of the above
boolean literals
Which of the following loop will execute the loop body at least one time? Check all that apply. while for do-while
do-while
Given the following input John Smith 323 Washington Ave What is the value ofaddressafter executing the following statements? firstName = stdin.next();lastName = stdin.next();address = stdin.nextLine(); 323 Washington Ave An empty string.
empty string
What's the value of the following expression: 55 / 10 * 10 == 55 false 55 not a valid expression true cannot be determined
false
Given the following input Coding is fun Java is powerful Practice is important What is the value of str2 after executing the following statements?str1 = stdin.next();str2 = stdin.next();str3 = stdin.next(); is Java is powerful Practice is important A blank space.
is
Given the following input Coding is fun Java is powerful Practice is important What is the value ofstr2after executing the following statements? str1 = stdin.next();str2 = stdin.nextLine();str3 = stdin.next(); is powerful is is fun Empty string.
is fun
What is the output of the following code? int num = 0; while (num < 4) { num = num + 1; System.out.println("num is " + num); } num is 1 num is 2 num is 3 num is 4 num is 5 num is 2 num is 3 num is 4 num is 1 num is 2 num is 3 num is 4 num is 1 num is 2 num is 3
num is 1 num is 2 num is 3 num is 4
Given an integer num that has been declared and initialized, use a while loop to determine whether or not num is a prime number or not. If yes, assign true to a boolean variable prime(declared already), otherwise, assign false to prime. code segment no complete program
prime = true; int factor = 2; while (num > factor){ if (num % factor == 0){ prime = false; } factor++; }
If a student will receive a B grade if his score is below 90, but better or equal to 80. Which of the following conditions that evaluate to true if the student does NOT receive a B grade? score >= 90 && score < 80 score <= 90 || score > 80 score >= 90 && score <= 80 score >= 90 || score < 80
score >= 90 || score < 80
What's the output of the following code segment? System.out.println("start"); int count = 1; do { System.out.println("count is " + count); count++; } while (count < 3); System.out.println("end");
start count is 1 count is 2 end
Which of the following method from the String class returns the n-th character from String variable str? str.char(n) str.str(n) str.charAt(n) str.at(n)
str.at(n)
Which of the following expressions are true if str1 contains the same sequence of characters as str2? str1.equals(str2) str1 = str2 str1 == str2 str1.compareTo(str2) == 0
str1.equals(str2) str1.compareTo(str2) == 0
In Java, the "greater than or equal to" operator is : "not equal" operator is :
the "greater than or equal to" operator is : >= "not equal" operator is : !=
What's the value of the following expressions: true || false : true && false : true && !false :
true || false : true true && false : false true && !false : true
Which of the following loops are pretest loops? while do-while for
while for
Which of the following expression determines whether or not x is a valid value for the following function √X^2 -4 / x - 3 x != 3 && (x >= 2 || x <= -2) x >3 || x <= -2 x != 3 || (x > 2 || x < -2) x != 3 && x => 2 && x =< -2
x != 3 && (x >= 2 || x <= -2)