No student devices needed. Know more
13 questions
Consider the code segment below
What is printed as a result?
int x = 10;
int y = 20;
System.out.print(y + x / y);
1
1.5
3
20
20.5
Which of the following expressions evaluates to 1?
1. 2 / 5 % 3
2. 2 / (5 % 3)
3. 2 / 5 + 1
1 only
2 only
1 and 2 only
2 and 3 only
1, 2, and 3
Consider the following:
int a = 2;
int b = 6;
int c = 3;
Which of the following expressions evaluates to false?
a < b == c < b
a > b == b < c
a < b != c < b
a < b != c < b
Consider the following code segment
boolean a = true;
boolean b = true;
System.out.print((b || (!a || b)) + " ");
System.out.print(((!b || !a) && a) + " ");
System.out.println(!(a && b) && b);
true false false
true true true
true false true
false true false
Consider the following code segment.
String str1 = new String("Happy");
String str2 = new String("Happy");
System.out.print(str1.equals(str2) + " ");
System.out.print(str2.equals(str1) + " ");
System.out.print(str1 == str2);
What is printed as a result of executing the code segment?
true true true
true true false
false true false
false false true
Consider the following code segment, which is intended to store the sum of all multiples of 10 between 10 and 100, inclusive (10 + 20 + ... + 100), in the variable total.
int x = 100;
int total = 0;
while( /* missing code */ )
{
total = total + x;
x = x - 10;
}
Which of the following can be used as a replacement for /* missing code */ so that the code segment works as intended?
x < 100
x <= 100
x > 10
x >= 10
Consider the following code segment.
int a = 1;
String result = "";
while (a < 20)
{
result += a;
a += 5;
}
System.out.println(result);
What, if anything, is printed as a result of executing the code segment?
21
161116
161161
Nothing is printing because of an infinite loop.
Consider the following code segment.
int count = 0;
int number = 20;
while (number > 0)
{
number = number / 2;
count++;
}
What will be the value of count after executing the code segment?
6
5
4
1
Consider the following code segment.
String word = "Computer";
int num = 3;
String result = "";
for (int k = num; k >= 0; k--)
{
result += word.substring(0, k);
}
System.out.print(result);
What is printed as a result of executing the code segment?
comcoc
ccocom
ccocomcomp
comcomcomcom
Consider the following code segment.
int a = 100;
while (a > 1)
{
System.out.println("$");
a /= 2;
}
How many $’s are printed as a result of executing the code segment?
0
5
6
7
50
Consider the following code segment.
String str = "AP";
str += "CS " + 1 + 2;
System.out.println(str);
What is printed as a result of executing the code segment?
CS AP12
AP CS3
CSAP 12
APCS 12
APCS 3
Consider the following code segment.
String one = "ABC123";
String two = "C";
String three = "3";
System.out.println(one.indexOf(two));
System.out.println(one.indexOf(three));
System.out.println(two.indexOf(one));
What is printed when the code segment is executed?
2
5
-1
2
5
2
2
6
-1
-1
-1
2
The code segment below is intended to randomly print one of the values 2, 4, 6, or 8 with equal probability.
int val = /* missing code */ ;
val *= 2;
System.out.print(val);
Which of the following can be used to replace /* missing code */ so that the code segment works as intended?
Math.random() * 4 + 1
Math.random() * 4
(int) (Math.random() * 4)
(int) (Math.random() * 4 + 1)
Explore all questions with a free account