Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

OOPS Fundamentals

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

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);

    }

}

a)

10

b)

5

c)

0

d)

Compilation Error

2.

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);

    }

}

a)

0

b)

100

c)

null

d)

Compilation Error

3.

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);

    }

}

a)

Default

b)

Alice

c)

null

d)

Compilation Error

4.

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);

    }

}

a)

0

b)

1

c)

2

d)

Compilation Error

5.

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();

    }

}

a)

Hello!

b)

hello!

c)

Compilation Error

d)

Nothing

6.

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);

    }

}

a)

12

b)

57

c)

5

d)

Compilation Error

7.

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);

    }

}

a)

100

b)

50

c)

0

d)

Compilation Error

8.

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();

    }

}

a)

Dog

b)

Animal

c)

Animal Dog

d)

Dog Animal

9.

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());

    }

}

a)

10

b)

20

c)

0

d)

Compilation Error

10.

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);

    }

}

a)

null

b)

" "

c)

Untitled

d)

Compilation Error