Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Exam (elaborations)

Some Practice Problems for the Java Exam and Solutions for the Problems

Rating
-
Sold
-
Pages
46
Grade
A+
Uploaded on
01-05-2025
Written in
2024/2025

Some Practice Problems for the Java Exam 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 .Scanner; Scanner scan = new Scanner(S); 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; Sln(++n ); Sln( n ); Sln( n++ ); Sln( n ); Sln( ‐n ); Sln( n ); ... Sln( ‐‐n ); Sln( n ); Sln( n‐‐ ); ... Sln( n ); Sln( n + k ); ... Sln( n ); Sln( k ); ... Sln("" + n + " " + k ); ... Sln( n ); ... Sln( " " + n ); ... Sln( " n" ); ... Sln( "n" ); ... Sln( " n * n = "); //CAREFUL! Sln( n * n ); ... Sln( 'n' ); ... }2. What is the output of the program below? public static void main(String[] args) { int n = 3; while (n = 0) { Sln( n * n ); ‐‐n; } Sln( n ); while (n 4) Sln( ++n ); Sln( n ); while (n = 0) Sln( (n /= 2) ); } 3. What is the output of the program below? public static void main(String[] args) { int n; Sln( (n = 4) ); Sln( (n == 4) ); Sln( (n 3) ); Sln( (n 4) ); Sln( (n = 0) ); Sln( (n == 0) ); Sln( (n 0) ); Sln( (n == 0 && true) ); Sln( (n == 0 || true) ); Sln( (!(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 = colorT; pants = colorT; Sln(" " + shirt + " " + pants ); } 5. What is the output when the following code fragment is executed? int i = 5, j = 6, k = 7, n = 3

Show more Read less
Institution
Course

Content preview

Some Practice Problems for the Java Exam
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.

Written for

Course

Document information

Uploaded on
May 1, 2025
Number of pages
46
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

$14.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
AcademicSuperScores Chamberlain College Of Nursing
Follow You need to be logged in order to follow users or courses
Sold
295
Member since
3 year
Number of followers
37
Documents
7648
Last sold
2 days ago
AcademicSuperScores

NURSING, ECONOMICS, MATHEMATICS, BIOLOGY AND HISTORY MATERIALS. BEST TUTORING, HOMEWORK HELP, EXAMS, TESTS AND STUDY GUIDE MATERIALS WITH GUARANTEE OF A+ I am a dedicated medical practitioner with diverse knowledge in matters Nursing and Mathematics. I also have an additional knowledge in Mathematics based courses (finance and economics)

4.5

155 reviews

5
124
4
9
3
11
2
5
1
6

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions