Which Java feature allows the same program to run on different operating systems without recompilation?
Answer: Platform independence / Write Once, Run Anywhere (WORA).
Which Java data type is used to store true or false?
Boolean
Which operator gives the remainder after division in Java?
%
What keyword is commonly used to create an object in Java?
New
What is automatic conversion from a smaller datatype to a larger compatible datatype called?
Widening conversion.
Which OOP concept hides implementation details and exposes only essential functionality?
Answer: Abstraction.
What is the default value of an instance variable of type int?
0
What is the output?
int x = 10; System.out.println(x > 5 ? "Yes" : "No");
Yes.
What special member of a class is automatically invoked when an object is created?
Answer: Constructor.
Is this valid?
int x = 25; double y = x;
Yes. int is automatically widened to double.
Name the four major principles of Object-Oriented Programming.
Answer: Encapsulation, Abstraction, Inheritance, Polymorphism.
What is the output?
int[] a = {10, 20, 30, 40};
System.out.println(a.length);4.
Predict the output:
int x = 5; System.out.println(x++ + ++x);
12.
Predict the output:
class Test {
int x = 10;
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x);
}
}10
What is the output?
double x = 9.8; int y = (int)x; System.out.println(y);
9.
A Car class keeps its data members private and provides public getter and setter methods. Which OOP principle is demonstrated?
Ans: Encapsulation
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.
How many times does this loop execute?
for(int i = 1; i <= 10; i *= 2)
System.out.println(i);
4
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.
Predict the output:
int x = 130; byte b = (byte)x; System.out.println(b);
-126.
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.
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
Predict the output:
int x = 10;
if(x > 5)
if(x < 8)
System.out.println("A");
else
System.out.println("B");B.
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.
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.