Term1 Java Exam 2024_2025 Marking Guide
Term1 Java Exam 2024_2025 Marking Guide
Maximum:100 Marks
Instructions:
1. The test is written
2. Follow the guidelines provided and seek clarifications if need be.
4. Collaboration and any kind or cheating is prohibited and should be resulted into a GOOD
ZERO.
INSTRUCTORS
class Test {
private int value = 42;
class Main {
public static void main(String[] args) {
Test test = new Test();
System.out.println(test.value);
}
}
a) 42
b)Compilation error
c) Runtime error
d) Undefined behavior
try {
int number = Integer.parseInt("XYZ");
} catch (NumberFormatException e) {
System.out.println("Invalid format");
}
8. Which principle ensures that a child class cannot directly access private members of a parent
class?
a) Encapsulation
b) Abstraction
c) Polymorphism
d) Inheritance
9. What will be the output of the following block of code?
class Base {
void display() {
System.out.println("Base display");
}
}
class Main {
public static void main(String[] args) {
Base obj = new Derived();
obj.display();
}
}
a) Base display
b) Derived display
c) Compilation error
d) Undefined behavior
10. What is the purpose of the Pattern class in Java Regex?
a) To store a compiled regex
b) To validate a regex string
c) To execute a regex match
d) To iterate over regex matches
11. What is the purpose of the ResultSet object in JDBC?
A) To execute SQL queries
B) To store the connection details
C) To store and manipulate the data returned by a query
D) To update the database
12. What does <T> represent in a generic class or method?
A. The type of a method parameter
B. A placeholder for a specific type
C. A superclass type
D. A static type
13. What is the output of the following java codes?
public class Test {
public static void main(String[] args) {
System.out.println(method());
}
}
A. Writes "Hello, world!" to a file named "test.txt"
B. Appends "Hello, world!" to an existing file named "test.txt"
C. Deletes "test.txt" and writes "Hello, world!"
D. Throws a runtime exception
16. Which of these is a valid declaration of a generic class?
A. public class Box<?>
B. public class Box<T>
C. public class Box<Generic>
D. Both B and C
17. Which regex will match a valid IP address?
a) ^(\\d{1,3}\\.){3}\\d{1,3}$
b) \\d+\\.\\d+\\.\\d+\\.\\d+
c) \\d{1,3}\\.{3}\\d{1,3}
d) None of the above
18. Which of the following statements is true for the given code?
24. Use try, catch and finally blocks to improve the method averageMark to handle
exceptions that can occur when the method is called.
class Program {
System.out.println(totalMarks/numberOfStudents);
averageMark(0,0);
averageMark(30,3);
class Program {
} catch (ArithmeticException e) {
} catch (Exception e) {
} finally {
super Keyword:
● Role: It refers to the superclass (parent class) of the current object. It is used to call
superclass methods, constructors, and to access superclass fields that are hidden by
subclass fields./ 1 Mark
● class Animal {
● }
● }
● System.out.println("Dog barks");
● }
● }
● dog.sound();
● }
● } / 1.5 Marks
27. What does the term WORA stand for, and why is Java considered to follow the WORA
principle?
WORA stands for "Write Once, Run Anywhere". It is a principle that signifies the
portability of Java programs. Java follows the WORA principle because Java code,
once compiled, can run on any platform that has a compatible Java Virtual Machine
(JVM), regardless of the underlying operating system or hardware architecture. This
is possible due to Java's architecture, which involves compiling the source code into
bytecode that is independent of the platform. The bytecode can then be executed on
any device that has the JVM installed, making Java highly portable.
Why Java follows the WORA principle:
1. Platform Independence: Java programs are compiled into bytecode (.class files) by
the Java compiler. This bytecode is not platform-specific, meaning the same
bytecode can run on any machine that has a JVM, which abstracts away the
platform-specific details.
2. JVM (Java Virtual Machine): The JVM interprets or compiles the bytecode into
machine-specific code for the underlying system. The JVM is available for various
platforms (Windows, Linux, MacOS, etc.), which enables Java programs to run on
those platforms without modification.
3. Java Libraries: Java's extensive standard libraries provide a consistent API, further
enhancing its portability. These libraries are designed to work uniformly across
platforms, contributing to the WORA principle.
Example:
If you write a Java program on a Windows system, you can compile the program into
bytecode. That bytecode can then be executed on any other system (like a Linux or
MacOS system) with the appropriate JVM installed, without needing any changes to
the code.
28. What is the difference between Collection and Collections? /5 Marks
Collection (interface): It is the root interface in the Java Collections Framework
that represents a group of objects. It defines basic methods like add(), remove(),
contains(), and size() for working with collections of objects. Common
implementations include List, Set, and Queue.
Collections (class): It is a utility class that provides static methods for operating on
or manipulating collections, such as sorting (sort()), reversing (reverse()), and
finding the maximum/minimum (max(), min()). It is not a collection itself but a
helper class for working with collections.
29. How does Java achieve multiple inheritance; provide an example to demonstrate this. / 5
Marks
Java does not support multiple inheritance (inheriting from more than one class)
directly through classes. However, Java achieves multiple inheritance through
interfaces. A class can implement multiple interfaces, allowing it to inherit behaviors
from multiple sources. This is a way to provide the benefits of multiple inheritance
while avoiding issues like the diamond problem (ambiguity in method
resolution)./2.5 Marks
interface Animal {
void eat();
}
interface Vehicle {
void drive();
}
31. Using examples, explain how an interface can have methods with implementation./5 Marks
In Java, an interface can have methods with implementation starting from Java 8
using the default and static keywords. These methods are provided with a default
behavior that implementing classes can either use directly or override.
1. default methods: A default method in an interface has a body (implementation). It
allows classes that implement the interface to inherit this default behavior without
needing to implement it themselves.
2. static methods: A static method in an interface can have an implementation, and it
belongs to the interface itself, not to the instances of the implementing classes.
interface Animal {
// Default method with implementation
default void sound() {
System.out.println("Animal makes a sound");
}
32. Create a student class with fields for student ID, first name, last name, and age. Apply the
principles of encapsulation to the class. Use either the Comparator or Comparable interface to
sort students based on first name, then last name, age and lastly the ID. Then, create three
Student objects and demonstrate the sorting functionality. / 15 Marks
Using Comparable Interface:
class Student implements Comparable{// 2 Marks
//3 Marks
private int studentId;
private String firstName;
private String lastName;
private int age;
// Constructor
public Student(int studentId, String firstName, String lastName, int age) {
this.studentId = studentId;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
// Implementing compareTo() for sorting based on first name, last name, age, and ID
@Override
public int compareTo(Student other) {
Student other=(Student)oth;
int result = this.firstName.compareTo(other.firstName);
if (result == 0) {
result = this.lastName.compareTo(other.lastName);
}
if (result == 0) {
result = Integer.compare(this.age, other.age);
}
if (result == 0) {
result = Integer.compare(this.studentId, other.studentId);
}
return result;
}
//Alternative
@Override
public int compareTo(Object o) {
Student other=(Student)o;
{
if(this.firstName.compareTo(other.firstName)){
return this.firstName.compareTo(other.firstName);
if(this.lastName.compareTo(other.lastName)){
return this.lastName.compareTo(other.lastName);
if(this.age.compareTo(other.age)){
return this.age.compareTo(other.age);
}
return this.studentId.compareTo(other.studentId);
}
@Override
public String toString() {
return studentId + ": " + firstName + " " + lastName + ", Age: " + age;
}
}
import java.util.*;
class Student {
private int studentId;
private String firstName;
private String lastName;
private int age;
// Constructor
public Student(int studentId, String firstName, String lastName, int age) {
this.studentId = studentId;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
@Override
public String toString() {
return studentId + ": " + firstName + " " + lastName + ", Age: " + age;
}
}
33. Create a Java program with an interface Animal class that contains a method makeSound().
The makeSound() method should be implemented in two subclasses, Cat and Dog, each
returning their respective sounds. Additionally, include a generic class SoundPrinter that
accepts any animal, which has a method printSound that takes an animal object as a
parameter and prints the sound made by the animal. Finally, implement a Main class to create
instances of Cat and Dog, and use the SoundPrinter class to print the sounds of these animals /
15 Marks