CT 1 Contents - OOPS - Java
CT 1 Contents - OOPS - Java
Why Java?
History of Java
Java Environment
The JDK (Java Development Kit) provides tools for developing, compiling, and debugging
Java programs.
A Java source file must contain at least one class definition, and it may include a public class
with a main method.
Compilation in Java
Java compilation generates bytecode in a .class file, which can be executed by the JVM.
class Example {
int x;
}
Constructors
Methods
Access Specifiers
Static Members
ClassName.methodName();
Final Members
The final keyword is used to declare constants, prevent method overriding, and prevent class
inheritance.
Comments
Single-line (//), multi-line (/* */), and documentation comments (/** */).
Data Types
Variables
Operators
The && (logical AND) operator returns true if both conditions are true.
Control Flow
while checks the condition before execution, whereas do-while executes at least once before
checking the condition.
java
class Test {
int x = 10;
public static void main(String[] args) {
Test obj = new Test();
System.out.println(obj.x);
}
}
Answer:
Output: 10
Explanation: x is an instance variable, and it is accessed using the object obj.
Answer:
Answer:
java
class Demo {
static int x = 10;
static {
x = 20;
}
public static void main(String[] args) {
System.out.println(x);
}
}
Answer:
Output: 20
Explanation: The static block executes before main(), updating x to 20.
Yes, we can overload main(), but JVM only calls public static void main(String[] args).
class Test {
public static void main(String[] args) {
int a = 5;
System.out.println(a++ + ++a);
}
}
Answer:
Output: 5 + 7 = 12
Explanation:
o a++ (post-increment) returns 5, then a becomes 6.
o ++a (pre-increment) increments a to 7 before returning it.
Answer:
Answer:
Output: 30Java1020
Explanation:
o 10 + 20 (addition) = 30
o "Java" is a string, so 30 + "Java" results in "30Java"
o "Java" + 10 results in "30Java10", and "30Java10" + 20 gives "30Java1020".
9. What will be the output of this code?
class Test {
public static void main(String[] args) {
int arr[] = new int[5];
System.out.println(arr[0]);
}
}
Answer:
Output: 0
Explanation: Arrays in Java are automatically initialized to default values (0 for int).
Answer:
class Test {
public static void main(String[] args) {
int x = 10;
int y = x++ + ++x;
System.out.println(y);
}
}
Answer:
Output: 10 + 12 = 22
Explanation:
o x++ (post-increment) returns 10, then x becomes 11.
o ++x (pre-increment) increases x to 12 before using it.
Answer:
class Demo {
public static void main(String[] args) {
int x = 5;
System.out.println(x > 2 ? x < 4 ? 10 : 8 : 7);
}
}
Answer:
Output: 8
Explanation:
o First condition (x > 2) is true, so it checks the second condition.
o x < 4 is false, so it returns 8.
class Test {
public static void main(String[] args) {
String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1 == s2);
}
}
Answer:
Output: false
Explanation:
o s1 is stored in the string pool, while s2 is created in heap memory.
o == checks memory reference, not value.
Answer:
java
import static java.lang.Math.*; // Static import of Math class
java
As you can see, static import removes the need to write Math. every time, making the code more
concise and readable. However, excessive use can reduce code clarity, especially if multiple static
imports create ambiguity.
3. Types of Exception
Q: What does the JVM do when an exception occurs and is not handled?
A: The JVM prints an error message, the exception type, and the stack trace, then terminates the
program.
Question:
Consider the following Java program. What will be the output, and how does the JVM react to the
exception?
Modify the code to handle the exception properly.
java
JVM Reaction: The program terminates abruptly because division by zero causes an
ArithmeticException, and no exception handler (try-catch) is provided.
java
Output:
csharp
Question:
The following program demonstrates checked and unchecked exceptions. Identify which exception is
checked and which is unchecked.
import java.io.*;
// Checked Exception
FileReader file = new FileReader("test.txt"); // Causes an exception
}
}
Answer:
java
import java.io.*;
try {
// Handling Checked Exception
FileReader file = new FileReader("test.txt");
} catch (FileNotFoundException e) {
System.out.println("Caught checked exception: " + e);
}
}
}
Question:
Analyze the control flow in the following program. Predict the output.
java
Answer:
Output:
mathematica
Explanation:
Question:
What will be the output of the following program? Modify the program to handle the exception using
throws.
java
Answer:
Output (without handling):
php
java
pgsql
5. User-defined Exception
Question:
Create a custom exception InvalidAgeException and use it in a Java program to validate age.
Answer:
java
Output:
php
Explanation: