Oral Java Question and Answer - New
Oral Java Question and Answer - New
1.Platform-independent
2.Object-oriented
4.Multithreaded
5.High-performance
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.
Java programs are compiled into bytecode, which can run on any system with a Java Virtual Machine
(JVM)
Example:
class Car {
String model;
void display() {
Example:
System.out.println("Hello, Java!");
Types:
1. Primitive Data Types: int, float, char, boolean, double, byte, short, long.
Example:
1. if statement
2. if-else statement
3. nested if statement
4. switch statement
The switch statement executes a block of code based on the value of a variable.
Example:
int day = 2;
switch (day) {
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.
Example:
System.out.println(numbers[1]); // Output: 20
Example:
String s = "Hello";
System.out.println(s.length()); // Output: 5
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).
Types
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?
The finalize() method is called before an object is garbage collected. It allows cleanup operations but
is not commonly used.
Exception Handling
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
Synchronization ensures that multiple threads do not access shared resources simultaneously,
preventing data inconsistency.
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?
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.
import java.awt.*;
import java.awt.event.*;
Button b;
AWTExample() {
b.addActionListener(this);
add(b);
setSize(300, 300);
setLayout(null);
setVisible(true);
System.out.println("Button Clicked!");
new AWTExample();
}
JDBC Questions
Q45. What is JDBC?
JDBC is an API in Java that allows interaction with databases using SQL queries.
5. Close connection.
import java.sql.*;
class JDBCExample {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
while (rs.next())
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.
1. Socket (Client-side)
2. ServerSocket (Server-side)
3. DataInputStream & DataOutputStream (for reading/writing data
Exception handling is a mechanism to handle runtime errors using try, catch, finally, throw, and
throws.