Share
Explore

20 Java Final Exam Review questions that progressively increase in complexity and cover a range of topics

basic programming constructs,
object-oriented concepts,
specific language features

Basic Program Structure: Write a simple Java program that prints "Hello, World!" to the console.
Variables and Types: Write a Java program that declares an integer variable, initializes it, and prints its value to the console.
Basic Input/Output: Modify the first program to read a user's name from the console and print "Hello, [name]!".
Control Structures - If/Else: Write a Java program that takes an integer from the user and prints whether it is odd or even.
Control Structures - For Loop: Write a Java program that prints the numbers from 1 to 10 using a for loop.
Control Structures - While Loop: Modify the above program to use a while loop instead of a for loop.
Arrays: Write a Java program that creates an array of 10 integers and initializes each element with its index value.
Basic Methods: Write a Java method that takes an integer array and returns the sum of its elements.
Class and Object: Define a Java class representing a 'Book' with fields for title, author, and ISBN. Write a method to display book details.
Method Calls and Field Access: Write a Java program where you create a Book object, set its fields, and display its details using the method from question 9.
Constructors: Modify the Book class to include a constructor that initializes all fields. Create a Book object using this constructor in your program.
Array of Objects: Write a Java program that creates an array of Book objects and initializes them with different values.
Passing Arrays to Methods: Write a method that takes an array of Book objects and prints the title of each book.
Inheritance: Define a new class 'Textbook' that extends 'Book' and adds a field for subject. Demonstrate inheritance by creating a Textbook object and calling its methods.
Method Overriding (Polymorphism): Override a method in the 'Textbook' class and demonstrate how it can be used polymorphically with a Book reference.
Abstract Class: Create an abstract class 'Publication' and refactor 'Book' and 'Textbook' to inherit from it, demonstrating the use of abstract methods.
Interface Implementation: Define an interface 'Printable' with a method 'printDetails()'. Implement this interface in the 'Book' class.
Exception Handling: Write a Java method that throws an arithmetic exception (like division by zero) and demonstrate how to handle this exception in your main program.
Collections: Write a Java program that uses a HashMap to store Book objects, with their ISBN as the key.
Advanced Object Composition: Create a class 'Library' that contains a collection of Book objects. Implement methods to add a Book to the Library, search for a Book by title, and list all books.
These questions cover a wide range of Java programming concepts, from the basics of syntax and control structures to more advanced topics like inheritance, polymorphism, and collections. They're designed to test not only the understanding of Java but also the ability to apply these concepts in practical programming scenarios.

I'll provide a brief solution for each of the Java test questions. Since the focus is on demonstrating key concepts, the solutions will be concise and focused on the primary learning objective of each question.
Challenge yourself to get these programs working and observe the results:
Hello, World!

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Variables and Types

public class IntegerVariable {
public static void main(String[] args) {
int number = 5;
System.out.println("The number is: " + number);
}
}

Basic Input/Output

import java.util.Scanner;

public class GreetUser {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
}
}

If/Else

import java.util.Scanner;

public class OddOrEven {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}

For Loop

public class CountToTen {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}

While Loop

public class CountToTenWhile {
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
System.out.println(i);
i++;
}
}
}

Arrays

public class InitializeArray {
public static void main(String[] args) {
int[] array = new int[10];
for (int i = 0; i < array.length; i++) {
array[i] = i;
}
}
}

Basic Methods

public class ArraySum {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
System.out.println("Sum: " + sumArray(array));
}

public static int sumArray(int[] array) {
int sum = 0;
for (int num : array) {
sum += num;
}
return sum;
}
}

Class and Object

public class Book {
String title;
String author;
String isbn;

void displayDetails() {
System.out.println("Title: " + title + ", Author: " + author + ", ISBN: " + isbn);
}
}

Method Calls and Field Access

public class TestBook {
public static void main(String[] args) {
Book myBook = new Book();
myBook.title = "Java Programming";
myBook.author = "Jane Doe";
myBook.isbn = "123456789";
myBook.displayDetails();
}
}

Constructors

public class Book {
String title;
String author;
String isbn;

public Book(String title, String author, String isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
}

void displayDetails() {
System.out.println("Title: " + title + ", Author: " + author + ", ISBN: " + isbn);
}
}

public class TestBook {
public static void main(String[] args) {
Book myBook = new Book("Java Programming", "Jane Doe", "123456789");
myBook.displayDetails();
}
}

Array of Objects

public class TestBookArray {
public static void main(String[] args) {
Book[] books = new Book[3];
books[0] = new Book("Java Programming", "Jane Doe", "123456789");
books[1] = new Book("Effective Java", "Joshua Bloch", "987654321");
books[2] = new Book("Clean Code", "Robert C. Martin", "192837465");

for (Book book : books) {
book.displayDetails();
}
}
}

Passing Arrays to Methods

public class TestBookArray {
public static void main(String[] args) {
Book[] books = new Book[3];
books[0] = new Book("Java Programming", "Jane Doe", "123456789");
books[1] = new Book("Effective Java", "Joshua Bloch", "987654321");
books[2] = new Book("Clean Code", "Robert C. Martin", "192837465");

printBookTitles(books);
}

public static void printBookTitles(Book[] books) {
for (Book book : books) {
System.out.println(book.title);
}
}
}

Inheritance

public class Textbook extends Book {
String subject;

public Textbook(String title, String author, String isbn, String subject) {
super(title, author, isbn);
this.subject = subject;
}
}

public class TestTextbook {
public static void main(String[] args) {
Textbook myTextbook = new Textbook("Algebra 101", "John Smith", "555555555", "Mathematics");
myTextbook.displayDetails();
System.out.println("Subject: " + myTextbook.subject);
}
}

Method Overriding (Polymorphism)

public class Textbook extends Book {
String subject;

public Textbook(String title, String author, String isbn, String subject) {
super(title, author, isbn);
this.subject = subject;
}

@Override
void displayDetails() {
System.out.println("Textbook Details: Title: " + title + ", Author: " + author + ", ISBN: " + isbn + ", Subject: " + subject);
}
}

public class TestPolymorphism {
public static void main(String[] args) {
Book myBook = new Textbook("Algebra 101", "John Smith", "555555555", "Mathematics");
myBook.displayDetails(); // Calls the overridden method in Textbook
}
}

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.