What is the output of the following program segment?
int num = 5;
while (num >= 0)
{
num -= 2;
}
System.out.println(num);
AP Computer Science Java Practice Exam #2
Quiz
•
Computers
•
9th - 12th Grade
•
Hard
Used 148+ times
FREE Resource
40 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
What is the output of the following program segment?
int num = 5;
while (num >= 0)
{
num -= 2;
}
System.out.println(num);
-2
-1
0
2
21
2.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
What is the output from
int n = 12;
System.out.print(goFigure(n));
System.out.print(" " + n);
where the method goFigure is defined as follows:
public double goFigure(int n)
{
n %= 7;
return (double) (12 / n);
}
2.0 12
2.4 12
2.0 5
2.4 5
2.4 6
3.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
Which of the following expressions will evaluate to true when x and y are boolean variables with different values?
I. (x || y) && (!x || !y)
II. (x || y) && !(x && y)
III. (x && !y) || (!x && y)
I only
II only
I and II only
II and III only
I, II, and III
4.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
What is the output from the following code segment?
double x = 5*4/2 - 5/2*4;
System.out.println(x);
0
1
0.0
1.0
2.0
5.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
What is the output from the following code segment?
String band = "anamanaguchi";
System.out.println(band.substring(1, 4)). compareTo(band.substring(5, 8)));
true
false
0
A negative integer
A positive integer
6.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
Integers can be represented in number systems with different bases. Fr example, 34(decimal) = 22(hexadecimal) = 100010(binary), where the subscripts "dec", "hex", and "bin" indicate the decimal (base 10), hexadecimal (base 16), and binary (base 2) systems, respectively. What are the hexadecimal and binary representations of 49(decimal)?
49(decimal) = 21(hexadecimal) = 110011(binary)
49(decimal) = 31(hexadecimal) = 11001(binary)
49(decimal) = 31(hexadecimal) = 110001(binary)
49(decimal) = 37(hexadecimal) = 110111(binary)
49(decimal) = 61(hexadecimal) = 110001(binary)
7.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
Questions 7-8 refer to the following method
public void printVals(String[] items, int k)
{
if(k > 1)
{
printVals(items, k - 1);
System.out.print(items[k] + " ");
printVals(items, k - 2);
}
}
Suppose the following code segment has been executed.
String[] names = {"Pat", "Joe", "Ann", "Cal", "Amy"};
printVals(names, name.length - 1);
7.What is the output?
Ann Cal Amy Ann
Ann Cal Amy Cal Ann
Ann Cal Joe Amy Joe Ann
Joe Ann Cal Joe Amy Joe Ann
Joe Ann Pat Cal Joe Amy Joe Ann Pat
41 questions
Midterm prep 7th
Quiz
•
7th Grade - University
35 questions
end of year Quiz
Quiz
•
12th Grade - University
40 questions
3 Tri C# 1
Quiz
•
12th Grade
43 questions
C++ Chapter 10
Quiz
•
9th - 12th Grade
45 questions
STRINGS
Quiz
•
11th Grade
40 questions
C Programming Midterm Review
Quiz
•
11th - 12th Grade
37 questions
AP CS A Midterm Review
Quiz
•
9th - 12th Grade
40 questions
Quizz PTS Kelas XI Struktur Data Array
Quiz
•
11th Grade - University
15 questions
Multiplication Facts
Quiz
•
4th Grade
25 questions
SS Combined Advisory Quiz
Quiz
•
6th - 8th Grade
40 questions
Week 4 Student In Class Practice Set
Quiz
•
9th - 12th Grade
40 questions
SOL: ILE DNA Tech, Gen, Evol 2025
Quiz
•
9th - 12th Grade
20 questions
NC Universities (R2H)
Quiz
•
9th - 12th Grade
15 questions
June Review Quiz
Quiz
•
Professional Development
20 questions
Congruent and Similar Triangles
Quiz
•
8th Grade
25 questions
Triangle Inequalities
Quiz
•
10th - 12th Grade
40 questions
Week 4 Student In Class Practice Set
Quiz
•
9th - 12th Grade
40 questions
SOL: ILE DNA Tech, Gen, Evol 2025
Quiz
•
9th - 12th Grade
20 questions
NC Universities (R2H)
Quiz
•
9th - 12th Grade
25 questions
Triangle Inequalities
Quiz
•
10th - 12th Grade
10 questions
Right Triangles: Pythagorean Theorem and Trig
Quiz
•
11th Grade
46 questions
Biology Semester 1 Review
Quiz
•
10th Grade
24 questions
LSO - Virus, Bacteria, Classification - sol review 2025
Quiz
•
9th Grade
65 questions
MegaQuiz v2 2025
Quiz
•
9th - 12th Grade