OOP Quiz Flashcards

1
Q

The following field has been declared as a member of a Java class.

int length;

Which of the following is correct?

‘length’ is class-level, and has an unknown initial value

‘length’ is class-level, and has an initial value of 0 Incorrect

‘length’ is class-level, and has an initial value of 1

‘length’ is instance-level, and has an initial value of 1
‘length’ is instance-level, and has an unknown initial value
‘length’ is instance-level, and has an initial value of 0

A

The correct answer is: ‘length’ is instance-level, and has an initial value of 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When writing a class, what is the return type declared in the signature of its constructor?

Select one:
void
The type being constructed
There is no return type declared

A

There is no return type declared Correct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an object-oriented programming concept in Java?

A

Inheritance
Polymorphism
Encapsulation
Narrowing conversion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In the ‘Rectangle’ class, I would like to define a method to compare two rectangles’ area. Which of the following is the best choice?

Select one:
a. Public static void compareArea(Rectangle rec1, Rectangle rec2)

b. Public static compareArea(Rectangle rec1, Rectangle rec2)
c. public static final int compareArea(Rectangle rec1, Rectangle rec2)
d. Public int compareArea(Rectangle rec1, Rectangle rec2)
e. public static int compareArea(Rectangle rec1, Rectangle rec2)

A

The correct answer is: public static int compareArea(Rectangle rec1, Rectangle rec2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Given a class Dog, which has a constructor which takes a single int argument (dog’s age), which of the below is syntactically correct in Java?

Select one:

a. Dog d = new Dog(int 5);
b. Dog d = Dog(5);
c. Dog d.age = 5;
d. Dog d = new Dog(5); Correct
e. Dog d = new Dog(5.0);

A

d. Dog d = new Dog(5);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The source file Test.java is as follows

class Test{

private int data=40;

public void msg(){

	System.out.println(“Data=“+data);

}

}

Which of the following is correct?

Select one:

a. It will compile successfully.
b. It will fail to compile, as the Test class need to add ‘public’.
c. It will fail to compile, as the data should not be initiated directly, in a constructor instead.
d. It will fail to compile, as the data is private, the msg() can’t access it.
e. It will fail to compile, as there is no constructor.

A

a. It will compile successfully. Correct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In the Q6’s code, how do we read the ‘data’ in another class?

Select one:
a. Test t;
     int a = t.data;
b. Test t = new test();
     int a = Test.msg();
c. The 'data' attribute is not accessible directly outside of 'Test' class.
d. int a = Test.data;
e. Test t = new Test();
     int a = t.data;
A

The ‘data’ attribute is not accessible directly outside of the ‘Test’ class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the contribution of the “new” keyword in the object creation in Java?

Select one:

a. Instantiation
b. Initialization
c. Declaration
d. Definition

A

c. Declaration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the advantages of encapsulation in OOP?

Select one:

a. supports maintenance
b. supports modularity
c. provides total control over data
d. all of the above

A

d. all of the above Correct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What components/constructs in Java support object oriented features?

Select one:

a. both method overloading and “Private” keyword
b. “Private” keyword
c. Switch statements
d. method overloading
e. For loops

A

a. both method overloading and “Private” keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly