Java Basics & OOP
Data Types & Variables
Operators & Control Flow
Classes & Objects
Type Conversion & Casting
100

Which Java feature allows the same program to run on different operating systems without recompilation?

Answer: Platform independence / Write Once, Run Anywhere (WORA).

100

Which Java data type is used to store true or false?

Boolean

100

Which operator gives the remainder after division in Java?

%

100

What keyword is commonly used to create an object in Java?

New

100

What is automatic conversion from a smaller datatype to a larger compatible datatype called?

Widening conversion.

200

Which OOP concept hides implementation details and exposes only essential functionality?

Answer: Abstraction.

200

What is the default value of an instance variable of type int?

0

200

What is the output?

int x = 10;
System.out.println(x > 5 ? "Yes" : "No");


Yes.

200

What special member of a class is automatically invoked when an object is created?

Answer: Constructor.

200

Is this valid?

int x = 25;
double y = x;


Yes. int is automatically widened to double.


300

Name the four major principles of Object-Oriented Programming.

Answer: Encapsulation, Abstraction, Inheritance, Polymorphism.

300

What is the output?

int[] a = {10, 20, 30, 40};
System.out.println(a.length);


4.

300

Predict the output:

int x = 5;
System.out.println(x++ + ++x);


12.

300

Predict the output:

class Test {
    int x = 10;
    public static void main(String[] args) {
        Test t = new Test();
        System.out.println(t.x);
    }
}


10

300

What is the output?

double x = 9.8;
int y = (int)x;
System.out.println(y);


9.

400

A Car class keeps its data members private and provides public getter and setter methods. Which OOP principle is demonstrated?

Ans: Encapsulation

400

What happens here?

public static void main(String[] args) {
    int x;
    System.out.println(x);
}


Compilation error because local variable x has not been initialized.

400

How many times does this loop execute?

for(int i = 1; i <= 10; i *= 2)   

 System.out.println(i);

4

400

What is wrong with this constructor?

class Student {
    void Student() {
        System.out.println("Student created");
    }
}


It is not a constructor because it has the return type void. It is a method named Student. A constructor must not have any return type.

400

Predict the output:

int x = 130;
byte b = (byte)x;
System.out.println(b);


-126.

500

A program has a parent class Shape with a method draw(). Circle and Rectangle redefine draw() differently. When draw() is called through a Shape reference, the appropriate subclass method executes. Identify the OOP concept.

Runtime polymorphism / Method overriding.

500

Predict the output:

int[] a = {2, 4, 6, 8};
int sum = 0;
for(int i = 0; i < a.length; i += 2)
    sum += a[i];
System.out.println(sum);


8

500

Predict the output:

int x = 10;
if(x > 5)
    if(x < 8)
        System.out.println("A");
    else
        System.out.println("B");


B.

500

output?class Demo {    int x;    Demo()

 {        x = 10;    }    

Demo(int x) {        this.x = x;    }    public static void main(String[] args) {        Demo d1 = new Demo();        Demo d2 = new Demo(25);        System.out.println(d1.x + d2.x);    } }

35.

500

Predict the output:

byte a = 10;
byte b = 20;
byte c = (byte)(a + b);
System.out.println(c);

Why is explicit casting required?

Answer: Output is 30. In Java, arithmetic operations involving byte, short, or char operands are promoted to int, so a + b produces an int. It must therefore be explicitly cast back to byte.