OOP Quiz 3 Flashcards
The following field has been declared as a member of a Java class.
int length;
Which of the following is correct?
Select one:
‘length’ is instance-level, and has an unknown initial value
‘length’ is instance-level, and has an initial value of 1
‘length’ is class-level, and has an initial value of 0
‘length’ is class-level, and has an unknown initial value
‘length’ is class-level, and has an initial value of 1
‘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:
There is no return type declared
void
The type being constructed
There is no return type declared
What is not an object oriented programming concept in Java?
Select one: Narrowing conversion Abstraction Polymorphism Inheritance Encapsulation
The correct answer is: 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 final int compareArea(Rectangle rec1, Rectangle rec2)
b. public static int compareArea(Rectangle rec1, Rectangle rec2)
c. Public static void compareArea(Rectangle rec1, Rectangle rec2)
d. Public int compareArea(Rectangle rec1, Rectangle rec2)
e. Public static compareArea(Rectangle rec1, Rectangle rec2)
b. 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(5.0);
b. Dog d = Dog(5);
c. Dog d = new Dog(int 5);
d. Dog d = new Dog(5);
e. Dog d.age = 5;
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 fail to compile, as the Test class need to add ‘public’.
b. It will fail to compile, as there is no constructor.
c. It will compile successfully.
d. It will fail to compile, as the data is private, the msg() can’t access it.
e. It will fail to compile, as the data should not be initiated directly, in a constructor instead.
The correct answer is: It will compile successfully.
In the Q6’s code, how do we read the ‘data’ in another class?
Select one:
a. The ‘data’ attribute is not accessible directly outside of ‘Test’ class.
b. Test t;
int a = t.data;
c. Test t = new Test(); int a = t.data;
d. Test t = new test(); int a = Test.msg();
e. int a = Test.data;
a. The ‘data’ attribute is not accessible directly outside of ‘Test’ class.
What is the contribution of the “new” keyword in the object creation in Java?
Select one:
a. Initialization
b. Instantiation
c. Declaration
d. Definition
b. Instantiation Correct
What are the advantages of encapsulation in OOP?
Select one:
a. all of the above Correct
b. provides total control over data
c. supports modularity
d. supports maintenance
a. all of the above
What components/constructs in Java support object oriented features?
Select one:
a. For loops
b. “Private” keyword
c. method overloading
d. both method overloading and “Private” keyword
e. Switch statements
d. both method overloading and “Private” keyword