MCQ Code Flashcards
- How would we write the first line of a class, Toyota, such that it extends the class, Car?
a) public Car extends Toyota{
b) public Toyota extends Car{
c) public Toyota implements Car{
d) public Car implements Toyota {
b) public Toyota extends Car{
- Consider the following code:
class X{ public void parentMethod(){ System.out.println(“parent”); } } class Y extends X{ public static void main(String args[]){ X x = new X(); x.parentMethod(); } public void myMethod(){ System.out.println(“child”); } }
What is the output of this code?
parent
- Suppose truck and motorcycle are subclasses of vehicle. Which of these declarations are invalid?
I Truck t = new Vehicle(); II Vehicle v = new Truck(); III Motorcycle m1 = new Vehicle(); IV Motorcycle m2 = new Truck();
I Truck t = new Vehicle(); III Motorcycle m1 = new Vehicle(); IV Motorcycle m2 = new Truck();
INVALID
- Which visibility modifier allows the data members of a superclass to be accessible to the instances of subclasses only?
a) public
b) private
c) packaged
d) protected
d) protected
- Given the following class definitions, Y and Z are called ___________.
class X {} class Y extends X{} class Z extends X{}
sibling classes
- What is wrong with the following code?
class super { public int a; protected int b; private int c; }
super is a reserved word and should not be used.
- What does the call, “super()” do?
a) It will do nothing.
b) Call the subclass’s constructor
c) Call the superclass’s constructor.
d) This is an invalid call.
c) Call the superclass’s constructor.
- Given the following class:
class MyClass { public MyClass(int x){ } }
Which of the following is invalid?
a) MyClass test = new MyClass(); will be a valid call since the compiler creates a default constructor automatically.
b) MyClass test = new MyClass(20); will be a valid call.
c) int y = 20;
MyClass test = new MyClass(y); will be a valid call.
d) All are valid.
INVALID: a) and d)
- In the following program:
class Truck extends Vehicle{ public Truck(){ super(); } }
What does super do?
c) Makes a call to the Vehicle() constructor.
- Which of the following types of methods cannot be declared as abstract?
a) Private methods
b) Static methods
c) a and b
d) neither a nor b
c) a and b
- True or false? Given the following class:
class X{ public X(int x){ } }
The following call is invalid
X b = new X();
Answer: True
- Assume we wish to represent a file named, “hello” with the symbol, x. How would we do it?
a) Directory x = new Directory(“hello”);
b) File x = new File(“hello”);
c) Directory hello = new File(x);
d) File hello = new File(x);
Answer: b
- Once we have opened a file for access in a Java program, which of the following objects is needed to write to the file?
a) FileInputStream
b) FileOutputStream
c) BufferedInputReader
d) BufferedStringWriter
Answer: b
- What does the following program do?
double x = 0, unknown;
for (int j = 0; j
c) Computes the average of all the doubles in the array, someArray.