OOP Quiz Flashcards
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
The correct answer is: ‘length’ is instance-level, and has an initial value of 0
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
There is no return type declared Correct
What is an object-oriented programming concept in Java?
Inheritance
Polymorphism
Encapsulation
Narrowing conversion
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)
The correct answer is: public static int compareArea(Rectangle rec1, Rectangle rec2)
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);
d. Dog d = new Dog(5);
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. It will compile successfully. Correct
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;
The ‘data’ attribute is not accessible directly outside of the ‘Test’ class.
What is the contribution of the “new” keyword in the object creation in Java?
Select one:
a. Instantiation
b. Initialization
c. Declaration
d. Definition
c. Declaration
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
d. all of the above Correct
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. both method overloading and “Private” keyword