1.
Program to find greatest of 3 numbers
In Java, we can compare three numbers using if-else ladder or nested if conditions.
The program checks the largest among three integers using logical operators.
Program:
public class Greatest {
public static void main(String[] args) {
int a = 15, b = 25, c = 10;
if(a >= b && a >= c)
System.out.println(a + " is greatest");
else if(b >= a && b >= c)
System.out.println(b + " is greatest");
else
System.out.println(c + " is greatest");
}
}
Explanation: Compares values using logical operators. The program correctly finds the largest
number.
2. Double loop to print 10 lines of Hello
Nested loops allow one loop inside another. The outer loop counts down from 10 to 1,
while the inner loop prints "Hello" once.
Program:
public class HelloLoop {
public static void main(String[] args) {
for(int i = 10; i >= 1; i--) {
for(int j = 1; j <= 1; j++) {
System.out.println("Hello");
}
}
}
}
Output: Prints Hello exactly 10 times.
3. Categories of Control Statements
Control statements direct program execution. Types:
1. Decision-making (if, if-else, switch)
2. Looping (for, while, do-while)
3. Jumping (break, continue, return)
Example:
if(x > 0) System.out.println("Positive");
4. Constructors
A constructor initializes objects. Same name as class, no return type.
Types: Default, Parameterized, Copy
Example:
class Student {
String name;
Student(String n) {
name = n;
}
}
5. Static Members
Declared with static keyword, belongs to class not objects.
Example:
class Counter {
static int count = 0;
Counter() { count++; }
static void show() {
System.out.println("Count = " + count);
}
}
6. Inheritance
Inheritance allows a class to acquire properties of another class using extends keyword.
Types: Single, Multilevel
Example:
class A {}
class B extends A {}
class C extends B {}
7. Abstract Class
Abstract class cannot be instantiated. May contain abstract methods.
Example:
abstract class Shape { abstract void draw(); }
class Circle extends Shape {
void draw() { System.out.println("Circle"); }
}
8. Packages
A package organizes classes and interfaces.
Creating package:
package mypack;
public class A { public void display(){ System.out.println("Hello"); } }
Accessing package:
import mypack.A;
9. Interfaces
Interface is pure abstraction with abstract methods.
Example:
interface Animal { void sound(); }
class Dog implements Animal {
public void sound() { System.out.println("Bark"); }
}
10. Exceptions & Handling
Exception is a runtime error. Handled using try-catch-finally.
Example:
try {
int x = 10/0;
} catch(ArithmeticException e) {
System.out.println("Division by zero");
}
11. Built-in Exceptions
Examples:
1. ArithmeticException
2. ArrayIndexOutOfBoundsException
3. NullPointerException
Example:
String s = null;
System.out.println(s.length());
12. Access Specifiers
Specifiers define scope of members.
Types:
- public
- private
- protected
- default
Example:
class Test {
public int a;
private int b;
protected int c;
int d;
}
13. Skip Program
This question is skipped / not applicable.