Java Viva Questions and Answers for End-Term
Practical Exam
CSE 310: Programming in Java
Prepared for Viva (40 Marks), Session 2024-25
May 5, 2025
Introduction
This document contains 32 questions and answers covering the syllabus of CSE 310: Pro-
gramming in Java, as well as a curated list of 19 high-probability viva questions (15 most
expected + 4 additional) to help you prepare for your end-term practical exam viva. The
questions are organized by syllabus units and practicals, with refined answers includ-
ing examples where applicable. Focus on the “Top 15 Most Expected Viva Questions”
and “Additional High-Probability Questions” to maximize your chances of scoring 35/40
marks.
1 All 32 Questions and Answers
The following questions are grouped by syllabus units and practicals, as per the CSE 310
syllabus.
1.1 Unit I: Java Basics
1. What is the difference between JDK, JRE, and JVM?
• JDK (Java Development Kit): Contains tools (e.g., javac) to write, compile,
and debug Java code.
• JRE (Java Runtime Environment): Includes JVM and libraries to run Java
programs.
• JVM (Java Virtual Machine): Executes Java bytecode, enabling platform
independence.
2. Why is Java platform-independent?
• Java compiles code to bytecode, not machine code.
• Bytecode runs on any system with a JVM, ensuring platform independence.
3. What is the main() method in Java?
• The entry point of a Java program.
• Syntax: public static void main(String[] args).
4. What are primitive data types in Java?
• Eight types: byte, short, int, long, float, double, char, boolean.
• Store simple values, not objects.
5. What is type casting in Java?
• Converting one data type to another (e.g., double to int).
1
• Example: int a = (int) 5.6; // Converts 5.6 to 55.6 to 5}.
1.2 Unit II: Loops, Arrays, OOP
1. Difference between for, while, and do-while loops?
• for: Used when the number of iterations is known.
• while: Checks condition first, runs if true.
• do-while: Runs at least once, checks condition after.
2. What is a constructor? How is it different from a method?
• Constructor: Initializes objects, has no return type, same name as class.
• Method: Performs tasks, has a return type, arbitrary name.
3. What is constructor overloading? itemize
4. Multiple constructors in a class with different parameters.
5. Java calls the matching constructor based on arguments.
6. What is the this keyword?
• Refers to the current object.
• Used to distinguish instance variables from local variables.
7. Difference between String and StringBuilder?
• String: Immutable, creates new objects on modification.
• StringBuilder: Mutable, efficient for frequent changes.
• Example: StringBuilder sb = new StringBuilder("Hello"); sb.append("
World");
1.3 Unit III: Inheritance and Polymorphism
1. What is inheritance?
• A class inherits fields/methods from another class using extends.
• Promotes code reuse and hierarchy.
2. What is method overriding?
• Subclass provides a specific implementation of a superclass method.
• Enables runtime polymorphism.
3. Difference between super and this?
• this: Refers to the current class instance.
• super: Refers to the superclass members.
4. What is polymorphism?
• Methods behave differently based on context.
• Types: Method overloading (compile-time), overriding (runtime).
2
1.4 Unit IV: Abstract Classes, Interfaces, Lambda
1. What is an abstract class?
• Cannot be instantiated, may have abstract and concrete methods.
2. Difference between abstract class and interface?
• Abstract class: Can have constructors, fields, and concrete methods.
• Interface (pre-Java 8): Only abstract methods; (Java 8+) default/static meth-
ods.
3. What is a functional interface?
• Interface with one abstract method.
• Used with lambda expressions.
4. What is a lambda expression?
• Concise function representation.
• Example: (a, b) -> a + b.
5. What is an anonymous class?
• Nameless class defined inline for one-time use.
• Example: new Runnable() { public void run() { } };
1.5 Unit V: Exceptions and I/O
1. What is exception handling in Java?
• Manages runtime errors using try, catch, finally.
• Prevents program crashes.
2. Difference between throw and throws?
• throw: Manually throws an exception.
• throws: Declares exceptions a method might throw.
3. What is try-with-resources?
• Automatically closes resources implementing AutoCloseable.
• Example: try (FileReader fr = new FileReader("file.txt")) { }
4. How do you serialize an object?
• Use ObjectOutputStream to save objects to a file.
• Class must implement Serializable.
3
1.6 Unit VI: Collections and JDBC
1. Difference between ArrayList and LinkedList?
• ArrayList: Fast access (O(1)), slow insert/delete (O(n)).
• LinkedList: Fast insert/delete (O(1)), slow access (O(n)).
2. Difference between HashMap and TreeMap?
• HashMap: Unordered key-value pairs.
• TreeMap: Sorted keys, slower operations.
3. What are generics in Java?
• Allow type-safe classes/methods.
• Example: ArrayList<String> list = new ArrayList<>();
4. What is JDBC? What are its steps?
• JDBC: Connects Java to databases.
• Steps: Load driver, connect, create statement, execute query, close.
1.7 Practical-Based Questions
1. How do you create a thread using lambda?
• Use Runnable r = () -> { /* code */ }; Thread t = new Thread(r);
t.start();
2. Difference between String and StringBuilder with example?
• String: Immutable, new objects on change.
• StringBuilder: Mutable, efficient.
• Example: StringBuilder sb = new StringBuilder("Hello"); sb.append("
World");
3. How do you handle file operations with exceptions?
• Use try-catch or try-with-resources to manage files.
• Example: try (BufferedWriter bw = new BufferedWriter(new FileWriter("file.tx
{ bw.write("Hello"); }
4. What are nested classes and their types?
• Classes inside another class.
• Types: Static nested, inner, local, anonymous.
5. What is Comparator used for in Collections?
• Defines custom sorting via compare() method.
• Example: Comparator<String> c = (s1, s2) -> s1.length() - s2.length();
4
2 Top 15 Most Expected Viva Questions with Refined Answers
These questions are selected for their alignment with the syllabus and practicals, with
refined answers for clarity.
1. What is the difference between JDK, JRE, and JVM?
• JDK: Development kit with tools (e.g., javac).
• JRE: Runtime environment with JVM and libraries.
• JVM: Executes bytecode, platform-independent.
2. Why is Java platform-independent?
• Compiles to bytecode, run by JVM on any OS.
3. What is the main() method in Java?
• Program entry point.
• Syntax: public static void main(String[] args).
4. What is a constructor, and how is it different from a method?
• Constructor: Initializes objects, no return type, same name as class.
• Method: Has return type, performs tasks.
• Example: class Student { Student() { } void display() { } }
5. What is the difference between String and StringBuilder?
• String: Immutable, new objects on change.
• StringBuilder: Mutable, efficient.
• Example: StringBuilder sb = new StringBuilder("Hello"); sb.append("
World");
6. What is inheritance, and why is it used?
• Subclass inherits from superclass via extends.
• Promotes code reuse.
• Example: class Animal { } class Dog extends Animal { }
7. What is method overriding, and how does it enable polymorphism?
• Subclass redefines superclass method.
• Enables runtime polymorphism.
• Example: class Animal { void sound() { } } class Dog extends Animal
{ void sound() { } }
8. What is the difference between an abstract class and an interface?
• Abstract class: Has constructors, fields, concrete methods.
• Interface: (Pre-Java 8) abstract methods; (Java 8+) default methods.
5
• Example: abstract class Shape { } interface Drawable { }
9. What is a lambda expression, and how is it used with functional inter-
faces?
• Concise function syntax.
• Used with functional interfaces.
• Example: Runnable r = () -> System.out.println("Run");
10. What is exception handling, and how do try, catch, and finally work?
• Handles errors with try, catch, finally.
• Example: try { int x = 10/0; } catch (ArithmeticException e) { }
finally { }
11. What is try-with-resources, and why is it useful?
• Auto-closes AutoCloseable resources.
• Example: try (FileReader fr = new FileReader("file.txt")) { }
12. What is the difference between ArrayList and LinkedList?
• ArrayList: Fast access (O(1)), slow insert/delete (O(n)).
• LinkedList: Fast insert/delete (O(1)), slow access (O(n)).
13. What is JDBC, and what are the steps to connect to a database?
• Connects Java to databases.
• Steps: Load driver, connect, create statement, execute query, close.
• Example: Class.forName("com.mysql.cj.jdbc.Driver");
14. How do you create a thread using a lambda expression?
• Use Runnable with lambda.
• Example: Runnable r = () -> { System.out.println("Thread"); }; Thread
t = new Thread(r); t.start();
15. How do you perform file read/write operations with exception handling?
• Use try-with-resources for file operations.
• Example: try (BufferedWriter bw = new BufferedWriter(new FileWriter("file.tx
{ bw.write("Hello"); }
3 Additional High-Probability Questions
1. What is the purpose of the static keyword in Java?
• Makes members belong to the class, not instances.
• Example: class Test { static int count = 0; } Test.count++;
6
2. What is the difference between method overloading and method over-
riding?
• Overloading: Same method name, different parameters, compile-time.
• Overriding: Subclass redefines superclass method, runtime.
• Example: Overloading: void add(int a, int b), void add(double a, double
b).
3. What is a Comparator, and how is it used in Collections?
• Defines custom sorting with compare().
• Example: Comparator<String> c = (s1, s2) -> s1.length() - s2.length();
Collections.sort(list, c);
4. What is serialization, and how do you implement it in Java?
• Converts objects to byte streams.
• Class implements Serializable.
• Example: try (ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream("file.ser"))) { oos.writeObject(obj); }
Preparation Tips
• Practice concise explanations (1–2 minutes per question).
• Use examples to clarify concepts.
• Be ready for follow-ups (e.g., “Why is this useful?”).
• Review your practical programs (e.g., exception handling, file I/O).
• Stay confident; if unsure, give a partial answer to show effort.