i) What is the output of the following: int x=2, 1;
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
System.out.print(i+j+x);
} x++;
System.out.println(" ");
}
j) What will be the output of the following code?
Answers
Answer:
Due to the print book page limit, we cannot inlcude all good CheckPoint questions in the physical book. The CheckPoint on this Website may contain extra questions not printed in the book. The questions in some sections may have been reordered as a result. Nevertheless, it is easy to find the CheckPoint questions in the book on this Website. Please send suggestions and errata to Dr. Liang at [email protected]. Indicate the book, edition, and question number in your email. Thanks!
Chapter 5 Check Point Questions
Section 5.2
▼5.2.1
Analyze the following code. Is count < 100 always true, always false, or sometimes true or sometimes false at Point A, Point B, and Point C?
int count = 0;
while (count < 100) {
// Point A
System.out.println("Welcome to Java!");
count++;
// Point B
}
// Point C
Show Answer Read Question
▼5.2.2
How many times are the following loop bodies repeated? What is the output of each loop?
(a)
int i = 1;
while (i < 10)
if (i % 2 == 0)
System.out.println(i);
(b)
int i = 1;
while (i < 10)
if (i % 2 == 0)
System.out.println(i++);
(c)
int i = 1;
while (i < 10)
if ((i++) % 2 == 0)
System.out.println(i);
Show Answer Read Question
▼5.2.3
What is the output of the following code? Explain the reason.
int x = 80000000;
while (x > 0)
x++;
System.out.println("x is " + x);
Show Answer Read Question
Section 5.3
▼5.3.1
What is wrong if guess is initialized to 0 in line 11 in Listing 5.3?
Show Answer Read Question
Section 5.4
▼5.4.1
Revise the code to increase the count only when the user gets a correct answer.
Show Answer Read Question
Section 5.5
▼5.5.1
Suppose the input is 2 3 4 5 0. What is the output of the following code?
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number, max;
number = input.nextInt();
max = number;
while (number != 0) {
number = input.nextInt();
if (number > max)
max = number;
}
System.out.println("max is " + max);
System.out.println("number " + number);
}
}
Show Answer Read Question
Section 5.6
▼5.6.1
Suppose the input is 2 3 4 5 0. What is the output of the following code?
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number, max;
number = input.nextInt();
max = number;
do {
number = input.nextInt();
if (number > max)
max = number;
} while (number != 0);
System.out.println("max is " + max);
System.out.println("number " + number);
}
}
Show Answer Read Question
▼5.6.2
What are the differences between a while loop and a do-while loop? Convert the following while loop into a do-while loop.
Scanner input = new Scanner(System.in);
int sum = 0;
System.out.println("Enter an integer " +
"(the input ends if it is 0)");
int number = input.nextInt();
while (number != 0) {
sum += number;
System.out.println("Enter an integer " +
"(the input ends if it is 0)");
number = input.nextInt();
}
Show Answer Read Question
Section 5.7
▼5.7.1
Do the following two loops result in the same value in sum?
(a)
for (int i = 0; i < 10; ++i) {
sum += i;
}
(b)
for (int i = 0; i < 10; i++) {
sum += i;
}
Show Answer Read Question
▼5.7.2
What are the three parts of a for loop control? Write a for loop that prints the numbers from 1 to 100.
Show Answer Read Question
▼5.7.3
Suppose the input is 2 3 4 5 0. What is the output of the following code?
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number, sum = 0, count;
for (count = 0; count < 5; count++) {
number = input.nextInt();
sum += number;
}
System.out.println("sum is " + sum);
System.out.println("count is " + count);
}