Question: Part A: Consider the class hierarchy shown below: -------------------------------------------------------------------- class FourWheeler implements DrivingUtilities class Car extends FourWheeler class Truck extends FourWheeler class Bus extends FourWheeler

Part A:

Consider the class hierarchy shown below:

--------------------------------------------------------------------

class FourWheeler implements DrivingUtilities

class Car extends FourWheeler

class Truck extends FourWheeler

class Bus extends FourWheeler

class Crane extends FourWheeler

----------------------------------------------------------------------

Consider the following code below:

1. DrivingUtilities du;

2. FourWheeler fw;

3. Truck myTruck = new Truck();

4. du = (DrivingUtilities)myTruck;

5. fw = new Crane();

6. fw = du;

Which of the statements below are true? (Choose 2)

Choices:

a. Line 4 will not compile because an interface cannot refer to an object.

b. The code will compile and run.

c. The code will not compile without an explicit cast at line 6, because going down the hierarchy without casting is not allowed.

d.The code at line 4 will compile even without the explicit cast.

e.The code will compile if we put an explicit cast at line 6 but will throw an exception at runtime.

Part B:

What results from trying to compile and run the following code?

1. import java.io.*;

2.

3. class MyClass

4. {

5. public static void main(String args[])

6. {

7. try

8. {

9. FileOutputStream fos = new FileOutputStream("abc");

10. DataOutputStream dos = new DataOutputStream(fos);

11. dos.writeByte(12);

12. fos.write(100);

13. fos.close();

14. dos.close();

15.

16. FileInputStream fis = new FileInputStream("abc");

17. DataInputStream dis = new DataInputStream(fis);

18. byte b = dis.readByte();

19. System.out.print(b + " ");

20. int i = dis.readInt();

21. System.out.println(i);

22. fis.close();

23. dis.close();

24. }

25. catch(IOException e)

26. {

27. System.out.println("An exception occurred");

28. }

29. }

30. }

Choices: (choose 1)

a. The output is 12 100

b. Compilation error at line 12 because once you chain a DataOutputStream onto the FileOutputStream, you can't write directly to the FileOutputStream.

c. An exception occurs at Run time at line 20 because there are only two bytes written in the file "abc" and the code tries to read a byte and then an integer.

d. Compilation error occurs at line 20 because there are only two bytes written in the file "abc" and the code tries to read a byte and then an integer.

Part C:

What is the output of the following program?

public class Gen

{

G g;

Gen(G g)

{

this.g =g;

}

public static void main(String[] args)

{

Gen arr[] = new Gen[5]; //line 1

arr[0] = new Gen("Java"); //line 2

arr[1] = new Gen(1); //line 3

arr[2] = (Gen)new Gen(1); //line 4

arr[3] = (Gen)new Gen(1); //line 5

for(Gen o:arr)

{

System.out.println(o);

}

}

}

a)Compile time Error at line 1

b)Compile time Error at line 3

c)Compile time Error at line 4

d)Compile time Error at line 5

e)Run Time Error

Part D:

What is the result of compiling and running the following code?

class VarArgOne {

public static void printArgs(String s, Integer ... i, String s) { //line 1

for(int j : i) { //line 2

System.out.print(j + " " + s); //line 3

}

}

public static void main(String ... args) { //line 4

printArgs("exam", 12, 34, "scjp"); //line 5

}

}

a) Compilation fails due to error at line 1.

b) Compilation fails due to error at line 2.

c) Compilation fails due to error at line 4.

d) Compilation fails due to error at both line 1 and line 4.

e) Compiles fine and Prints output "12 scjp 34 scjp".

Part E:

1. public class Item {

2. private String desc;

3. public String getDescription() { return desc; }

4. public void setDescription(String d) { desc = d; }

5.

6. public static void modifyDesc(Item item, String desc) {

7. item = new Item();

8. item.setDescription(desc);

9. }

10. public static void main(String[] args) {

11. Item it = new Item();

12. it.setDescription(Gobstopper);

13. Item it2 = new Item();

14. it2.setDescription(Fizzylifting);

15. modifyDesc(it, Scrumdiddlyumptious);

16. System.out.println(it.getDescription());

17. System.out.println(it2.getDescription());

18. }

19. }

What is the outcome of the code?

A. Compilation fails.

B. Gobstopper Fizzylifting

C. Gobstopper Scrumdiddlyumptious

D. Scrumdiddlyumptious Fizzylifltng

E. Scrumdiddlyumptious Scrumdiddlyumptious

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!