Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

OOP final

Total questions: 59

Worksheet time: 15hrs 45mins

Name
Class
Date
1.

Which is a valid keyword in?

a)

interface

b)

string

c)

Float

d)

unsigned

2.

Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?

a)

final

b)

static

c)

private

d)

protected

3.

Which is a valid declarations of a String?

a)

String s1 = null;

b)

String s2 = 'null';

c)

String s3 = (String) 'abc';

d)

String s4 = (String) '\ufeed';

4.

Which cannot directly cause a thread to stop executing?

a)

Calling the SetPriority() method on a Thread object.

b)

Calling the wait() method on an object

c)

Calling notify() method on an object.

d)

Calling read() method on an InputStream object.

5.

Which statement is true for the following code? 

public class Plant {  private String name; public Plant(String name) { this.name = name; } public String getName() { return name;} } public class Tree extends Plant { public void growFruit() { } public void dropLeaves() { } }

a)

The code will compile without changes.

b)

The code will compile if public Tree() { Plant(); } is added to the Tree class.

c)

The code will compile if public Plant() { Tree(); } is added to the Plant class.

d)

The code will compile if public Plant() { this("fern"); } is added to the Plant class.

6.

You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability?

a)

java.util.Map

b)

java.util.Set

c)

java.util.List

d)

java.util.Collection

7.

Which constructs an anonymous inner class instance?

a)

Runnable r = new Runnable() { };

b)

Runnable r = new Runnable(public void run() { });

c)

Runnable r = new Runnable { public void run(){}};

d)

System.out.println(new Runnable() {public void run() { }});

8.

Which of the following will directly stop the execution of a Thread?

a)

wait()

b)

notify()

c)

notifyall()

d)

exits synchronized code

9.

You can create an ArrayList using

a)

ArrayList()

b)

new ArrayList()

c)

new ArrayList[ ]

d)

new ArrayList[100]

10.

The default value for data field of a boolean type, numeric type, object type is , respectively.

a)

false, 1, null

b)

true, 0, null

c)

true, 1, Null

d)

true, 1, null

e)

false, 0, null

11.

Show the output of the following code:

public class Test { public static void main(String[ ] args){ int[ ] x = {1, 2, 3, 4, 5};increase(x);int[ ] y = {1, 2, 3, 4, 5};increase(y[0]);System.out.println(x[0] + ʺ ʺ + y[0]);}public static void increase(int[ ] x){ for (int i = 0; i < x.length; i++) x[i]++;}public static void increase(int y) { y++; }}

a)

2 1

b)

2 2

c)

1 2

d)

1 1

e)

0 0

12.

Analyze the following fragment:

double sum = 0;double d = 0;while (d != 10.0){d += 0.1; sum += sum + d; }

a)

The program never stops because d is always 0.1 inside the loop.

b)

The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.

c)

After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9

d)

The program does not compile because sum and d are declared double, but assigned with integer value 0.

13.

Which class or interface defines the wait(), notify(),and notifyAll() methods?

a)

Object

b)

Thread

c)

Runnable

d)

Class

14.

Which will legally declare, construct, and initialize an array?

a)

int [] myList = {"1", "2", "3"};

b)

int [] myList = (5, 8, 2);

c)

int myList [] [] = {4,9,7,0};

d)

int myList [] = {4, 3, 7};

15.

Which of the following statements creates an instance of File on Window for the file c:\t.txt?

a)

new File(ʺc://txt.txtʺ)

b)

new File(ʺc:/txt.txtʺ)

c)

new File(ʺc:\\txt.txtʺ)

d)

new File(ʺc:\txt.txtʺ)

16.

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]?

a)

x.add(ʺChicagoʺ)

b)

x.add(1, ʺChicagoʺ)

c)

x.add(0, ʺChicagoʺ)

d)

x.add(2, ʺChicagoʺ)

17.

Which of the following is the correct header of the main method?

a)

public static void main(String x[ ])

b)

public static void main(String[ ] args)

c)

public static void main(String args[ ])

d)

static void main(String[ ] args)

e)

public static void main(String[ ] x)

18.

When invoking a method with an object argument, _______ is passed.

a)

the reference of the object

b)

a copy of the object

c)

the object is copied, then the reference of the copied object

d)

the contents of the object

19.

To assign a double variable d to a float variable x, you write

a)

x = (int)d;

b)

x=d;

c)

x=(long)d

d)

x=(float)d;

20.

What is the name of the method used to start a thread execution?

a)

int();

b)

start();

c)

run();

d)

resume();

21.

Suppose a method p has the following heading:public static int[ ][ ] p()What return statement may be used in p()?

a)

return int[ ]{1, 2, 3};

b)

return new int[ ][ ]{{1, 2, 3}, {2, 4, 5}};

c)

return new int[ ]{1, 2, 3};

d)

return {1, 2, 3};

e)

return 1;

22.

Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?

a)

java.util.SortedMap

b)

java.util.TreeMap

c)

java.util.TreeSet

d)

java.util.Hashtable

23.

You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?

a)

public

b)

protected

c)

private

d)

transient

24.

Which cause a compiler error?

a)

int[ ] scores = {3, 5, 7};

b)

int [ ][ ] scores = {2,7,6}, {9,3,45};

c)

String cats[ ] = {"Fluffy", "Spot", "Zeus"};

d)

boolean results[ ] = new boolean [] {true, false, true};

25.

Which statement is true for the following code?

 public class Plant { private String name; public Plant(String name) { this.name = name; } public String getName() { return name; }}public class Tree extends Plant { public void growFruit() { } public void dropLeaves() { }}

a)

The code will compile without changes.

b)

Тhe code will compile if public Tree() { Plant(); } is added to the Tree class.

c)

The code will compile if public Plant() { Tree(); } is added to the Plant class.

d)

The code will compile if public Plant() { this("fern"); } is added to the Plant class.

26.

Which will contain the body of the thread?

a)

run();

b)

start();

c)

stop();

d)

main();

27.

What is the output of the following code:

public class Test { public static void main(String[ ] args){ Object o1 = new Object();Object o2 = new Object();System.out.print((o1 == o2) + ʺ ʺ + (o1.equals(o2)));}}

a)

false true

b)

false false

c)

true true

d)

true false

28.

Which class does not override the equals() and hashCode() methods, inheriting them directly from class Object?

a)

java.lang.String

b)

java.lang.Double

c)

java.lang.StringBuffer

d)

java.lang.Character

29.

What is the number of iterations in the following loop:for (int i = 1; i < n; i++){ // iteration }

a)

n + 1

b)

n - 1

c)

2*n

d)

n

30.

Which interface provides the capability to store objects using a key-value pair?

a)

Java.util.Map

b)

Java.util.Set

c)

Java.util.List

d)

Java.util.Collection

31.

What will be the output?

public class Base { public static final String FOO = "foo"; public static void main(String[] args) { Base b = new Base(); Sub s = newSub(); System.out.print(Base.FOO); System.out.print(Sub.FOO); System.out.print(b.FOO); System.out.print(s.FOO); System.out.print(((Base)s).FOO);}} class Sub extends Base { public static final String FOO="bar";}

a)

foofoofoofoofoo

b)

foobarfoobarbar

c)

foobarfoofoofoo

d)

foobarfoobarfoo

e)

barbarbarbarbar

32.

_________ is invoked to create an object.

a)

A method with a return type

b)

The main method

c)

A constructor

d)

A method with the void return type

33.

Object-oriented programming allows you to derive new classes from existing classes. This is called .

a)

generalization

b)

abstraction

c)

encapsulation

d)

inheritance

34.

Analyze the following code fragments that assign a boolean value to the variable even.Code 1:if (number % 2 == 0)even = true;elseeven = false;Code 2:even = (number % 2 == 0) ?true: false;Code 3:even = number % 2 == 0;

a)

Code 2 has a compile error, because you cannot have true and false literals in the conditional expression.

b)

All three are correct, but Code 2 is preferred.

c)

All three are correct, but Code 1 is preferred.

d)

All three are correct, but Code 3 is preferred.

e)

Code 3 has a compile error, because you attempt to assign number to even.

35.

The following code displays.double temperature = 50;if (temperature >= 100) System.out.println(ʺtoo hotʺ);else if (temperature <= 40) System.out.println(ʺtoo coldʺ);else System.out.println(ʺjust rightʺ);

a)

too cold

b)

just right

c)

too hot

d)

too hot too cold just right

36.

Which one is a valid declaration of a boolean?

a)

boolean b1 = 0;

b)

boolean b2 = 'false';

c)

boolean b3 = false;

d)

boolean b4 = Boolean.false();

37.

Which method registers a thread in a thread scheduler?

a)

run();

b)

construct();

c)

start();

d)

register();

38.

What is the printout of the following code:double x = 5.5;int y = (int)x;System.out.println(ʺx is ʺ + x + ʺ and y is ʺ + y);

a)

x is 5.5 and y is 5.0

b)

x is 6 and y is 6

c)

x is 5 and y is 6

d)

x is 5.5 and y is 5

e)

x is 6.0 and y is 6.0

39.

Which is true about a method-local inner class?

a)

It must be marked final.

b)

It can be marked abstract.

c)

It can be marked public.

d)

It can be marked static.

40.

Which is the valid declarations within an interface definition?

a)

public double methoda();

b)

public final double methoda();

c)

static void methoda(double d1);

d)

protected void methoda(double d1);

41.

what is true about methods in a final class

a)

all methods in a final class are implicitly protected

b)

all methods in a final class are implicitly abstract

c)

all methods in a final class are should be explicitly marked as final

d)

all methods in a final class are implicitly final

42.

All classes defined without an explicit extends clause automatically extend

a)

Instance superclass 

b)

Nothing 

c)

Inheritance class

d)

Object superclass

43.

Which method helps to get the value of a private field?

a)

getter

b)

none of them

c)

setter

d)

both of them

44.

Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?

a)

java.util.ArrayList

b)

java.util.HashSet

c)

java.util.LinkedHashSet

d)

java.util.List

45.

You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability?

a)

java.util.List

b)

java.util.Map

c)

java.util.Set

d)

java.util.Collection

46.

Choose right sequence of steps to work with database

a)

Define the connection URL -&gt; Establish the connection -&gt; Process the result -&gt; Execute a query -&gt; Create the Statement object -&gt;   Close the connection

b)

Define the connection URL -&gt; Establish the connection -&gt;Execute a query -&gt; Create the Statement object -&gt;  Process the result -&gt; Close the connection

c)

Establish the connection -&gt; Define the connection URL -&gt; Create the Statement object -&gt; Execute a query -&gt; Process the result -&gt; Close the connection

d)

Define the connection URL -&gt; Establish the connection -&gt; Create the Statement object -&gt; Execute a query -&gt; Process the result -&gt; Close the connection

47.

Which of the following statements about public access modifier is true?

a)

accessible everywhere

b)

accessible in the same package and by subclasses

c)

accessible only inside the same class

d)

accessible in the same package

48.

These patterns are concerned with how classes and objects can be composed, to form larger structures

a)

Creational

b)

Behavioral

c)

Structural

49.

Which one is a valid declaration of a boolean?

a)

boolean b1 = 0;

b)

boolean b4 = Boolean.false();

c)

boolean b3 = false;

d)

boolean b2 = 'false';

50.

In the following code, what is the printout for list2?class Test { public static void main(String[ ] args){ int[ ] list1 = {1, 2, 3};int[ ] list2 = {1, 2, 3};list2 = list1;list1[0] = 0;list1[1] = 1;list2[2] = 2;for (int i = 0; i < list2.length; i++)System.out.print(list2[i] + ʺ ʺ); } }

a)

0 1 2

b)

1 1 1

c)

0 1 3

d)

1 2 3

51.

You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?

a)

default access

b)

public

c)

protected

d)

private

52.

Suppose you wish to provide an accessor method for a boolean property finished, what signature of the method should be?

a)

public void getFinished()

b)

public boolean getFinished()

c)

public boolean isFinished()

d)

public void isFinished()

53.

Classes consist of:

a)

Fields, Constructors, Functions

b)

Fields, Constructors, Parameters, Methods

c)

Parameters, Constructors, Methods

d)

Fields, Constructors, Methods

54.

Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?

a)

reeMap

b)

LinkedHashMap

c)

The answer depends on the implementation of the existing instance.

d)

HashMap

55.

Which of the following will not directly cause a thread to stop?

a)

InputStream access

b)

sleep()

c)

wait()

d)

notify()

56.

If one object is-part-of another object e.g. CPU is a part of a PC, then relationship between them is ...

a)

Aggregation

b)

Composition

c)

Association

d)

Inheritance

57.

By usage of operator SUPER we

a)

get an access to any of the superclass members (methods, variables, constructors)

b)

get an access to only superclass methods 

c)

get an access to only superclass constructors

d)

get an access to only superclass methods and constructors

58.

What is k after the following block executes?{ int k = 2; nPrint(ʺA messageʺ, k); }System.out.println(k);

a)

1

b)

2

c)

k is not defined outside the block. So, the program has a compile error

d)

0

59.

Classes consist of:

a)

Fields, Constructors, Functions

b)

Fields, Constructors, Parameters, Methods

c)

Parameters, Constructors, Methods

d)

Fields, Constructors, Methods