0% found this document useful (0 votes)
13 views10 pages

Oral Java Question and Answer - New

The document provides a comprehensive overview of core Java concepts, including definitions of Java, its features, and essential components like JDK, JRE, and JVM. It covers object-oriented programming principles, data types, exception handling, multithreading, and JDBC, along with examples and explanations of key terms and functionalities. Additionally, it discusses AWT for GUI development and socket programming for network communication.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

Oral Java Question and Answer - New

The document provides a comprehensive overview of core Java concepts, including definitions of Java, its features, and essential components like JDK, JRE, and JVM. It covers object-oriented programming principles, data types, exception handling, multithreading, and JDBC, along with examples and explanations of key terms and functionalities. Additionally, it discusses AWT for GUI development and socket programming for network communication.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Core Java Concepts


Q1. What is Java?

A: Java is an object-oriented, platform-independent, high-level programming language that follows


the WORA (Write Once, Run Anywhere) principle and developed by James Gosling at Sun
Microsystems in 1995.

Q2. What are the features of Java?

1.Platform-independent

2.Object-oriented

3.Robust and secure

4.Multithreaded

5.High-performance

6.Dynamic and interpreted

Q3. Explain the difference between JDK, JRE, and JVM.

1. JDK (Java Development Kit): Includes JRE + Development tools (compiler, debugger, etc.).
2. JRE (Java Runtime Environment): Contains JVM + Libraries required for running Java
applications.
3. JVM (Java Virtual Machine): Converts Java bytecode into machine code.

Q4. What are the four pillars of OOP?

1. Encapsulation – Wrapping data and methods into a single unit (class).

2. Abstraction – Hiding implementation details and exposing only functionality.

3. Inheritance – Acquiring properties of one class in another.

4. Polymorphism – Ability to take multiple forms (method overloading & overriding).

Q5 Why is Java called platform-independent?

Java programs are compiled into bytecode, which can run on any system with a Java Virtual Machine
(JVM)

Q6. What is a class in Java?

A class is a blueprint for creating objects. It defines variables and methods.

Example:
class Car {

String model;

void display() {

System.out.println("Car Model: " + model);

Q7. What is an object in Java?

An object is an instance of a class. It has state (variables) and behavior (methods).

Example:

Car myCar = new Car(); // Creating an object

myCar.model = "Toyota"; // Assigning value

myCar.display(); // Calling method

Q8. What is the main() method in Java?

The main() method is the entry point of a Java program.

public class Main {

public static void main(String[] args) {

System.out.println("Hello, Java!");

Q9. What are variables in Java? What are its types?

A variable stores a value in memory.

Types:

1. Local Variable – Inside a method, only accessible there.

2. Instance Variable – Defined inside a class, but outside methods.

3. Static Variable – Defined using static keyword, shared across objects.

Q10. What are data types in Java?

Java has two types of data types:

1. Primitive Data Types: int, float, char, boolean, double, byte, short, long.

2. Non-Primitive Data Types: String, Array, Class, Interface.


Q11. What is the difference between primitive and non-primitive data types?

Q12. What are the different types of operators in Java?

1. Arithmetic Operators (+, -, *, /, %)

2. Relational Operators (==, !=, >, <, >=, <=)

3. Logical Operators (&&, ||, !)

4. Bitwise Operators (&, |, ^, ~, <<, >>)

5. Assignment Operators (=, +=, -=, *=, /=, %=)

6. Unary Operators (++, --)

Q12. What is the difference between ‘==’ and ‘equals()’ in Java?

== checks if two references point to the same object.

equals() checks if two objects have equal values.

Example:

String s1 = new String("Java");

String s2 = new String("Java");

System.out.println(s1 == s2); // false (different memory locations)

System.out.println(s1.equals(s2)); // true (same value)

Q13. What are conditional statements in Java?

1. if statement

2. if-else statement

3. nested if statement

4. switch statement

Q14. What is a switch statement?

The switch statement executes a block of code based on the value of a variable.

Example:

int day = 2;

switch (day) {

case 1: System.out.println("Monday"); break;

case 2: System.out.println("Tuesday"); break;


default: System.out.println("Invalid day");

Q15. What are the different types of loops in Java?

1. for loop – Executes a block of code multiple times.

2. while loop – Repeats while the condition is true.

3. do-while loop – Executes at least once, then checks the condition.

Q16. What is the difference between while and do-while loop?

The main difference between a while loop and a do-while loop lies in when the condition is checked:
while loops check the condition before executing the loop body, potentially skipping it entirely if the
condition is initially false,
while do-while loops execute the loop body once before checking the condition, guaranteeing at least
one execution.

Q17. What is an array in Java?

An array is a collection of elements of the same type stored in contiguous memory.

Example:

int[] numbers = {10, 20, 30}; // Array initialization

System.out.println(numbers[1]); // Output: 20

Q18. What is a string in Java?

A String is a sequence of characters, implemented as an immutable object.

Example:

String s = "Hello";

System.out.println(s.length()); // Output: 5

Q19. What is a method in Java?

A method is a block of code that performs a task.

Example:

void greet() {

System.out.println("Hello, Java!");

}
Q20. What is the difference between call by value and call by reference?

Call by Value – The method gets a copy of the variable (Java uses this).

Call by Reference – The method gets a reference to the original variable (Not used in Java).

Q21. What are access modifiers in Java?

1. private – Accessible only within the class.

2. default – Accessible within the same package.

3. protected – Accessible in the same package and subclasses.

4. public – Accessible everywhere.

Q22. What is the final keyword in Java?

The final keyword can be used to:

1. Make a variable constant – final int x = 10;

2. Prevent method overriding – final void show() { }

3. Prevent class inheritance – final class Car { }

Q23. What is the ‘super’ keyword in Java?

super refers to the parent class and is used to:

1. Call the parent class constructor.

2. Access parent class methods and variables.

Q24. What is a constructor? What are its types?

A constructor is a special method used to initialize an object.

Types

1. Default Constructor – No parameters.

2. Parameterized Constructor – Accepts parameters.

3. Copy Constructor – Copies values from another object (manually implemented).

Q25. What is the difference between static and instance variables?

1.Static variables belong to the class and are shared among all objects.

2.Instance variables belong to individual objects and have separate values for each instance.
Q26. How is memory managed in Java?

Java memory is divided into:

1. Stack – Stores method calls and local variables.

2. Heap – Stores objects and instance variables.

3. Method Area – Stores class metadata and static variables.

Q27. What is garbage collection in Java?

Garbage collection (GC) automatically deallocates memory occupied by unreachable objects to


avoid memory leaks.

Q28. What is the finalize() method?

The finalize() method is called before an object is garbage collected. It allows cleanup operations but
is not commonly used.

Exception Handling

Q29. What is an exception?

An exception is an unwanted or unexpected event that disrupts program execution.

Q30. What are checked and unchecked exceptions?

1. Checked exceptions (Compile-time) – Must be handled (e.g., IOException, SQLException).


2. Unchecked exceptions (Runtime) – Caused by logic errors (e.g., NullPointerException,
ArithmeticException).

Q31. How do you handle exceptions in Java?

Using try, catch, finally, throw, and throws.

Q32. What is the difference between throw and throws?

throw – Used inside a method to explicitly throw an exception (throw new ArithmeticException();).

throws – Declares an exception in a method signature (public void method() throws IOException {}).
Multithreading

Q33. What is a thread in Java?

A thread is the smallest unit of execution in a program.

Q34. What are the ways to create a thread in Java?

1. Extending Thread class

2. Implementing Runnable interface

Q35. What is the difference Between process and thread


A process is a program in execution, while a thread is a lightweight unit of execution within a
process, A process has its own memory space and resources, while threads share the memory
space of the process they are part of. .

Q36. What is synchronization in Java?

Synchronization ensures that multiple threads do not access shared resources simultaneously,
preventing data inconsistency.

Q37. How do you achieve synchronization in Java?

1. Using synchronized keyword (synchronized method/ block).

2. Using Locks (ReentrantLock class).

Q38.What is the difference between Stack and Heap memory in Java?

1. Stack memory: Stores method calls and local variables.


2. Heap memory: Stores objects and instance variables.

Q39. What is the difference between abstract class and interface?

In Java, both abstract classes and interfaces are used for abstraction, but they differ in their purpose
and capabilities. Abstract classes can have both abstract and concrete methods, while interfaces
contain only abstract methods (and since Java 8, also default and static methods

AWT Questions
Q40. What is AWT in Java?

AWT is a set of APIs in Java used to create Graphical User Interfaces (GUIs), containing components
like buttons, labels, and text fields.
Q41. What are the basic components of AWT?

1. Container (Frame, Panel, Dialog)


2. Components (Button, Label, TextField, Checkbox, List)
3. Layout Managers (FlowLayout, BorderLayout, GridLayout)

Q42. What is an event in AWT?

An event is an action performed by the user, like clicking a button or pressing a key. Java uses the
Event Delegation Model to handle events.

Q43. How do you handle events in AWT?

Using EventListener Interfaces like ActionListener, MouseListener, KeyListener, etc.

Q44. How do you create a simple AWT program with a button?

import java.awt.*;

import java.awt.event.*;

class AWTExample extends Frame implements ActionListener {

Button b;

AWTExample() {

b = new Button("Click Me");

b.setBounds(50, 100, 80, 30);

b.addActionListener(this);

add(b);

setSize(300, 300);

setLayout(null);

setVisible(true);

public void actionPerformed(ActionEvent e) {

System.out.println("Button Clicked!");

public static void main(String args[]) {

new AWTExample();
}

JDBC Questions
Q45. What is JDBC?

JDBC is an API in Java that allows interaction with databases using SQL queries.

Q46. What are the steps to connect Java with a database?

1.Load the JDBC Driver: Class.forName("com.mysql.cj.jdbc.Driver");

2. Establish connection: Connection con = DriverManager.getConnection(URL, USER, PASSWORD)

3. Create a statement: Statement stmt = con.createStatement();

4. Execute query: ResultSet rs = stmt.executeQuery("SELECT * FROM table");

5. Close connection.

Q47. What are the types of JDBC drivers?

1. Type-1 (JDBC-ODBC Bridge Driver)


2. Type-2 (Native-API Driver)
3. Type-3 (Network Protocol Driver)
4. Type-4 (Thin Driver - Pure Java Driver) (Most commonly used)

Q48. Write a simple JDBC program to fetch records from a database.

import java.sql.*;

class JDBCExample {

public static void main(String args[]) {

try {

Class.forName("com.mysql.cj.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb",


"root", "password");

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT * FROM students");

while (rs.next())

System.out.println(rs.getInt(1) + " " + rs.getString(2));

con.close();
} catch (Exception e) {

System.out.println(e);

Socket Programming
Q49. What is Socket Programming in Java?

Socket programming enables communication between two machines over a network using TCP/IP
protocols.

Q50. What are the classes used in Socket programming?

1. Socket (Client-side)
2. ServerSocket (Server-side)
3. DataInputStream & DataOutputStream (for reading/writing data

Q51. What is multithreading in Java?

Multithreading allows concurrent execution of two or more threads to improve performance.

Q52. How do you create a thread in Java?

1. By extending Thread class


2. By implementing Runnable interface

Q53. What is exception handling in Java?

Exception handling is a mechanism to handle runtime errors using try, catch, finally, throw, and
throws.

Q54. What is polymorphism in Java?

Polymorphism allows a single method to perform different tasks. It is of two types:

1. Compile-time (Method Overloading)


2. Runtime (Method Overriding

You might also like