WorksheetsOOPS Fundamentals
Total questions: 10
Worksheet time: 5mins
What will be the output?
class Box {
int length = 5;
}
public class Main {
public static void main(String[] args) {
Box b1 = new Box();
Box b2 = new Box();
b2.length = 10;
System.out.println(b1.length);
}
}
10
5
0
Compilation Error
What is the output?
class Test {
int num;
Test() {
num = 100;
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.num);
}
}
0
100
null
Compilation Error
What is the output?
class Person {
String name;
Person() {
name = "Default";
}
Person(String n) {
name = n;
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person("Alice");
System.out.println(p.name);
}
}
Default
Alice
null
Compilation Error
What will the following code print?
class Counter {
int count = 0;
void increment() {
count++;
}
}
public class Main {
public static void main(String[] args) {
Counter c1 = new Counter();
c1.increment();
c1.increment();
System.out.println(c1.count);
}
}
0
1
2
Compilation Error
What will the code output?
class Greet {
void sayHello() {
System.out.println("Hello!");
}
}
public class Main {
public static void main(String[] args) {
Greet g = new Greet();
g.sayHello();
}
}
Hello!
hello!
Compilation Error
Nothing
What will the output be?
class Point {
int x;
Point(int xVal) {
x = xVal;
}
}
public class Main {
public static void main(String[] args) {
Point p1 = new Point(5);
Point p2 = new Point(7);
System.out.println(p1.x + p2.x);
}
}
12
57
5
Compilation Error
What is the output?
class Sample {
int data = 50;
void changeData() {
int data = 100;
}
}
public class Main {
public static void main(String[] args) {
Sample s = new Sample();
s.changeData();
System.out.println(s.data);
}
}
100
50
0
Compilation Error
What is the output?
class Animal {
Animal() {
System.out.print("Animal ");
}
}
class Dog extends Animal {
Dog() {
System.out.print("Dog ");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
}
}
Dog
Animal
Animal Dog
Dog Animal
What is the output?
class Sum {
int a;
void setA(int val) {
a = val;
}
int getA() {
return a;
}
}
public class Main {
public static void main(String[] args) {
Sum s1 = new Sum();
Sum s2 = new Sum();
s1.setA(10);
s2.setA(20);
System.out.println(s1.getA());
}
}
10
20
0
Compilation Error
What will the code print?
class Book {
String title;
Book() {
title = "Untitled";
}
}
public class Main {
public static void main(String[] args) {
Book b = new Book();
System.out.println(b.title);
}
}
null
" "
Untitled
Compilation Error
