Java Test – Answer Key
Part A – Concept-Based Theory
1. A software company wants to develop an application that runs on Windows,
Linux, and macOS without changing the source code. Which feature of Java
makes this possible? Explain.
Answer:
Java's Platform Independence makes this possible. Java programs are compiled into bytecode,
which is executed by the JVM. Therefore, the same program can run on any operating system
that has a JVM.
2. Your friend has installed only the JRE but wants to write and compile Java
programs. Will it work? Justify your answer.
Answer:
No. JRE is used only to run Java programs. To write and compile Java programs, JDK (Java
Development Kit) must be installed because it contains the Java compiler (javac) and other
development tools.
3. A Java program compiles successfully but fails to execute because the JVM is
missing. What is the role of the JVM in Java?
Answer:
The JVM (Java Virtual Machine) executes Java bytecode, allocates memory, and enables
platform independence. Without the JVM, Java programs cannot run.
4. A developer creates a class named Student but forgets to create an object. Can
the methods of the class be accessed? Explain your answer.
Answer:
No. An object must be created to access the non-static methods and variables of a class because
memory is allocated only when an object is created.
5. A hospital application stores patient details such as name, age, and weight.
Suggest suitable Java data types for each field and justify your choice.
Answer:
Field Data Type Reason
Name String Stores text
Age int Stores whole numbers
Weight float or double Stores decimal values
6. A user enters the name "Rahul Kumar". Which Scanner method should be
used to read the complete name? Why is next() not suitable?
Answer:
Use nextLine() because it reads the entire line including spaces.
next() reads only the first word and ignores the remaining words after the space.
7. A school has a Person class, and both Student and Teacher classes need to use
the properties of Person. Which OOP concept is suitable? Mention two
advantages.
Answer:
The suitable OOP concept is Inheritance.
Advantages:
Code Reusability
Reduces Code Redundancy
8. A class contains three methods named display() with different parameter lists.
Which OOP concept is demonstrated? Explain.
Answer:
This is Method Overloading.
Method Overloading allows multiple methods with the same name but different parameters
(number, type, or order) in the same class.
9. An online banking application hides the account balance from direct access
and allows users to access it only through methods. Which OOP concept is used?
State its purpose.
Answer:
The concept is Encapsulation.
Purpose:
Protects data
Provides data hiding
Improves code security and maintainability
10. A student wants to compare two usernames entered by the user without
considering uppercase and lowercase letters. Which String method should be
used? Why?
Answer:
Use equalsIgnoreCase() because it compares two strings without considering uppercase and
lowercase letters.
Part B – Programming Answers
11. Student Result
import [Link];
public class StudentResult {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Student Name: ");
String name = [Link]();
[Link]("Enter Mark 1: ");
int m1 = [Link]();
[Link]("Enter Mark 2: ");
int m2 = [Link]();
[Link]("Enter Mark 3: ");
int m3 = [Link]();
int total = m1 + m2 + m3;
[Link]("Student Name: " + name);
[Link]("Total Marks: " + total);
if (m1 >= 40 && m2 >= 40 && m3 >= 40)
[Link]("Result: Pass");
else
[Link]("Result: Fail");
}
}
12. Positive, Negative or Zero
import [Link];
public class NumberCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a Number: ");
int num = [Link]();
if (num > 0)
[Link]("Positive Number");
else if (num < 0)
[Link]("Negative Number");
else
[Link]("Zero");
}
}
13. Employee Class and Object
class Employee {
void displayDetails() {
[Link]("Employee Name: Arun");
[Link]("Department: IT");
}
}
public class Demo {
public static void main(String[] args) {
Employee emp = new Employee();
[Link]();
}
}
14. String Operations
import [Link];
public class StringDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a String: ");
String str = [Link]();
[Link]("Length: " + [Link]());
[Link]("Uppercase: " + [Link]());
if ([Link]("Java"))
[Link]("Contains Java");
else
[Link]("Does Not Contain Java");
}
}