and Solutions for the Problems
The problems below are not intended to teach you how to program in Java. You should not attempt
them until you believe you have mastered all the topics on the "Checklist" in the document entitled "C101
Java Study Guide".
There are 40 problems. The solutions for the problems are given at the end, after the statement of
problem 40.
In all these problems, we’ll assume that the following has been done in the appropriate places:
import java.util.Scanner;
Scanner scan = new Scanner(System.in);
1. What is the exact output of the program below. Indicate a blank line in the output by writing blank
line .
public static void main(String[] args)
{
int n = 4, k = 2;
System.out.println(++n );
System.out.println( n );
System.out.println( n++ );
System.out.println( n );
System.out.println( ‐n );
System.out.println( n ); ...
System.out.println( ‐‐n );
System.out.println( n );
System.out.println( n‐‐ ); ...
System.out.println( n );
System.out.println( n + k ); ...
System.out.println( n );
System.out.println( k ); ...
System.out.println("" + n + " " + k ); ...
System.out.println( n ); ...
System.out.println( " " + n ); ...
System.out.println( " n" ); ...
System.out.println( "\n" ); ...
System.out.println( " n * n = "); //CAREFUL!
System.out.println( n * n ); ...
System.out.println( 'n' ); ...
}
,2. What is the output of the program below?
public static void main(String[] args)
{
int n = 3;
while (n >= 0)
{
System.out.println( n * n );
‐‐n;
}
System.out.println( n );
while (n < 4)
System.out.println( ++n );
System.out.println( n );
while (n >= 0)
System.out.println( (n /= 2) );
}
3. What is the output of the program below?
public static void main(String[] args)
{
int n;
System.out.println( (n = 4) );
System.out.println( (n == 4) );
System.out.println( (n > 3) );
System.out.println( (n < 4) );
System.out.println( (n = 0) );
System.out.println( (n == 0) );
System.out.println( (n > 0) );
System.out.println( (n == 0 && true) );
System.out.println( (n == 0 || true) );
System.out.println( (!(n == 2) ));
}
,4. What is the output of the following program?
enum colorType {red, orange, yellow, green, blue, violet};
public static void main(String[] args)
{
colorType shirt, pants;
shirt = colorType.red;
pants = colorType.blue;
System.out.println(" " + shirt + " " + pants );
}
5. What is the output when the following code fragment is executed?
int i = 5, j = 6, k = 7, n = 3;
System.out.println( i + j * k ‐ k % n );
System.out.println( i / n );
6. What is the output when the following code fragment is executed?
int found = 0, count = 5;
if (!found || ‐‐count == 0)
System.out.println( "danger" );
System.out.println( "count = " + count );
7. What is the output when the following code fragment is executed?
char [] title = {'T', 'i', 't', 'a', 'n', 'i', 'c'};
ch = title[1];
title[3] = ch;
System.out.println( title );
System.out.println( ch );
, 8. Suppose that the following code fragment is executed.
String message;
System.out.println( "Enter a sentence on the line below." );
message = scan.next();
System.out.println( message );
a. Suppose that in response to the prompt, the interactive user types the following line and presses Enter:
Please go away.
What will the output of the code fragment look like?
b. Suppose that we modify the program and use scan.nextLine() instead of scan.next(), and that
in response to the prompt, the interactive user types the following line and presses Enter:
Please stop bothering me.
What will the output of the code fragment look like?
9. Write a for loop that outputs all the even numbers between 0 and 20 in reverse order, from the largest
to the smallest.
10. Write a for loop that outputs all the odd numbers between 0 and 20 in reverse order, from the largest
to the smallest.
11. The nested conditional statement shown below has been written by an inexperienced Java
programmer. The behavior of the statement is not correctly represented by the formatting.
if (n < 10)
if (n > 0)
System.out.println("The number is positive.");
else
System.out.println("The number is ______________.");
a. What is the output of the statement if the variable n has the value 7 ? If n has the value 15 ?
If n has the value -3 ?
b. Correct the syntax of the statement so that the logic of the corrected statement corresponds to the
formatting of the original statement. Also, replace the blank with an appropriate word or phrase.
c. Correct the formatting of the (original) statement so that the new format reflects the logical behavior
of the original statement. Also, replace the blank with an appropriate word or phrase.