MCQ Code Flashcards

1
Q
  1. 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 {

A

b) public Toyota extends Car{

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. 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?

A

parent

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. 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();
A
I   Truck t = new Vehicle();
III Motorcycle m1 = new Vehicle();
IV Motorcycle m2 = new Truck();

INVALID

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. 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

A

d) protected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Given the following class definitions, Y and Z are called ___________.
class X {}
class Y extends X{}
class Z extends X{}
A

sibling classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. What is wrong with the following code?
class super {
  public int a;
  protected int b;
  private int c;
  }
A

super is a reserved word and should not be used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. 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.

A

c) Call the superclass’s constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. 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.

A

INVALID: a) and d)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. In the following program:
class Truck extends Vehicle{
  public Truck(){
    super();
}
}

What does super do?

A

c) Makes a call to the Vehicle() constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. 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

A

c) a and b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. True or false? Given the following class:
class X{
  public X(int x){
  }  
}

The following call is invalid

X b = new X();

A

Answer: True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. 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);

A

Answer: b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. 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

A

Answer: b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. What does the following program do?

double x = 0, unknown;

for (int j = 0; j

A

c) Computes the average of all the doubles in the array, someArray.

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