Midterm Part 2
What is the output of the following code? //Program "A" public class A { public static void main(String[] args) { fooBar(3); } public static int fooBar(int n) { int result = 0; if (n <= 0) { result = n; } else { result = fooBar(n - 1) + n; } System.out.println(result); return n; } }
0 1 3 5
Given the following code, what will be the value of finalAmount when it is displayed? public class Order { private int orderNum; private double orderAmount; private double orderDiscount; public Order(int orderNumber, double orderAmt, double orderDisc) { orderNum = orderNumber; orderAmount = orderAmt; orderDiscount = orderDisc; } public double finalOrderTotal() { return orderAmount - orderAmount * orderDiscount; } } public class CustomerOrder { public static void main(String[] args) { Order order; int orderNumber = 1234; double orderAmt = 580.00; double orderDisc = .1; order = new Order(orderNumber, orderAmt, orderDisc); double finalAmount = order.finalOrderTotal(); System.out.printf("Final order amount = $%,.2f\n", finalAmount); } } a. 522.00 b. 528.00 c. 580.00 d. There is no value because the object, order, has not been created
a. 522.00
In the following code, which line has an error? Line 1 public interface Interface1 Line 2 { Line 3 int FIELDA = 55; Line 4 public int methodA(double){} Line 5 } a. Line 1 b. Line 2 c. Line 3 d. Line 4
d. Line 4
Given the following code: Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(int a){} Line 5 } Line 6 public class ClassB extends ClassA Line 7 { Line 8 public ClassB(){} Line 9 public void method1(){} Line 10 } Line 11 public class ClassC extends ClassB Line 12 { Line 13 public ClassC(){} Line 14 public void method1(){} Line 15 } Which method will be executed when the following statements are executed? ClassC item1 = new ClassA(); item1.method1(); a. Line 14 b. Line 4 c. Line 9 d. This is an error and will cause the program to crash
d. This is an error and will cause the program to crash
In the following code, which line in ClassA has an error? Line 1 public interface MyInterface Line 2 { Line 3 int FIELDA = 55; Line 4 public int methodA(double); Line 5 } Line 6 public class ClassA implements MyInterface Line 7 { Line 8 FIELDA = 60; Line 9 public int methodA(double) { } Line 10 } a. Line 6 b. Line 7 c. Line 8 d. Line 9
c. Line 8
What is the output of the following code? //Program "B" public class B { public static void main(String[] args) { fooBar(4); } public static int fooBar(int n) { int result = 1; if (n <= 0) { result = n; System.out.println(result); } else { for (int i = 1; i <= n; i++) { result *= i; System.out.println(result); } } return n; } }
1 2 6 24
What is the output of the following code? //Program "C" /** File C.java */ public class C extends D { public static void main(String[] args) { C myObj = new C("Ford Mustang", 10000.00); myObj.run(); } private void run() { System.out.println("I owe $" + this + "."); doSomething(); System.out.println("I owe $" + this + "."); doSomething(10000.00); System.out.println("I owe $" + this + "."); } public C() { super("Unknown", 0.00); } public C(String name, double owed) { super(name, owed); } public void doSomething() { super.doSomething(100.00); } } /** File D.java */ public class D { protected String name; protected double owed; public D() { this("Unknown", 100.00); } public D(String name, double owed) { this.name = name; this.owed = owed; } public void doSomething(double payment) { owed -= payment; } public void doSomething() { owed *= 1.10; } public String toString() { String str = String.format("%.2f", owed); return str; } }
I owe $10000.00. I owe $9900.00. I owe $8900.00.
In the following code, which line will cause a compiler error? Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public int method1(int a){} Line 5 public final int method2(double b){} Line 6 } Line 7 public ClassB extends ClassA Line 8 { Line 9 public ClassB(){} Line 10 public int method1(int b){} Line 11 public int method2(double c){} Line 12 } a. Line 10 b. Line 4 c. Line 11 d. Line 5
a. Line 10
The generic method public static <E extends Number> void displayArray(E[] array) { for (E element : array) System.out.println(element); } can be passed: a. an array whose element type is Integer b. an array whose element type is Object c. an array whose element type is E d. an array whose element type is any superclass of Number
a. an array whose element type is Integer
If you have defined a class, SavingsAccount, with a public static data member named numberOfAccounts, and created a SavingsAccount object referenced by the variable account20, which of the following will assign numberOfAccounts to numAccounts? a. numAccounts = SavingsAccount.numberOfAccounts; b. numAccounts = numOfAccounts; c. numAccounts = account20; d. numAccounts = account20.numAccounts;
a. numAccounts = SavingsAccount.numberOfAccounts;
In the following code, what is missing from ClassA? Line 1 public interface MyInterface Line 2 { Line 3 int FIELDA = 55; Line 4 public int methodA(double); Line 5 } Line 6 public class ClassA implements MyInterface Line 7 { Line 8 FIELDA = 60; Line 9 public int methodB(double) { } Line 10 } a. It does not overload methodA b. It does not override methodA c. It does not have a constructor d. Nothing is missing. It is a complete class
b. It does not override methodA
In the following code, what will the call to super do? public class ClassB extends ClassA { public ClassB() { super(40); System.out.println("This is the last statement "+ "in the constructor."); } } a. This cannot be determined from the code. b. The method super will have to be defined before we can say what will happen. c. It will call the constructor of ClassA that receives an integer as an argument. d. It will call the method super and pass the value 40 to it as an argument.
c. It will call the constructor of ClassA that receives an integer as an argument.
Given the following code, what will be the value of finalAmount when it is displayed? public class Order { private int orderNum; private double orderAmount; private double orderDiscount; public Order(int orderNumber, double orderAmt, double orderDisc) { orderNum = orderNumber; orderAmount = orderAmt; orderDiscount = orderDisc; } public int getOrderAmount() { return orderAmount; } public int getOrderDisc() { return orderDisc; } } public class CustomerOrder { public static void main(String[] args) { int ordNum = 1234; double ordAmount = 580.00; double discountPer = .1; Order order; double finalAmount = order.getOrderAmount() - order.getOrderAmount() * order.getOrderDisc(); System.out.printf("Final order amount = $%,.2f\n", finalAmount); } } a. 580.00 b. 528.00 c. There is no value because the constructor has an error d. There is no value because the object, order, has not been created
d. There is no value because the object, order, has not been created
Given the following code: Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(int a){} Line 5 } Line 6 public class ClassB extends ClassA Line 7 { Line 8 public ClassB(){} Line 9 public void method1(){} Line 10 } Line 11 public class ClassC extends ClassB Line 12 { Line 13 public ClassC(){} Line 14 public void method1(){} Line 15 } Which method1 will be executed when the following statements are executed? ClassA item1 = new ClassB(); item1.method1(); a. method1 on Line 14 b. method1 on Line 4 c. method1 on Line 9 d. This is an error and will cause the program to crash
d. This is an error and will cause the program to crash