Question: PART 1 Consider the class hierarchy shown below: -------------------------------------------------------------------- class FourWheeler implements DrivingUtilities class Car extends FourWheeler class Truck extends FourWheeler class Bus extends FourWheeler
PART 1
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:
DrivingUtilities du;
FourWheeler fw;
Truck myTruck = new Truck();
du = (DrivingUtilities)myTruck;
fw = new Crane();
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 2
What results from trying to compile and run the following code?
import java.io.*; 2.
class MyClass
{
public static void main(String args[])
{
try
{
FileOutputStream fos = new FileOutputStream("abc");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeByte(12);
fos.write(100);
fos.close();
dos.close();
FileInputStream fis = new FileInputStream("abc");
DataInputStream dis = new DataInputStream(fis);
byte b = dis.readByte();
System.out.print(b + " ");
int i = dis.readInt();
System.out.println(i);
fis.close();
dis.close();
}
catch(IOException e)
{
System.out.println("An exception occurred");
}
}
}
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 3
What is the result of compiling and running the following code?
class VarArgOne { public static void printArgs(String s, Integer ... i, String s) { //LINE1
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. c) Compilation fails due to error at both line 1 and line 4. d) Compiles fine and Prints output "12 scjp 34 scjp".
PART 4
public class Item {
private String desc;
public String getDescription() { return desc; }
public void setDescription(String d) { desc = d; }
5.
public static void modifyDesc(Item item, String desc) {
item = new Item();
item.setDescription(desc);
}
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
Get step-by-step solutions from verified subject matter experts
