please explain, thanks!
1.What will be the result of attempting to run the following program?
public class Qaa75 {
public static void main(
String args[]) {
String[][][] arr = {
{ {}, null },
{ { "1", "2" }, { "1", null, "3" } },
{},
{ { "1", null } }
};
System.out.println(arr.length + arr[1][2].length);
}
}
A]Program will terminate withArrayOutOfBounds Exception
B]Program will terminate withNullPointer Exception
C]4 printed to output
D]6 printed to output
E]7 printed to standard output
2.Which code fragments will succeed in printing the last argument given on the command line to the standard output, and exit gracefully with no output if no arguments are given?
CODE FRAGMENT A:
public static void main(String args[]) {
if (args.length != 0)
System.out.println(args[args.length-1]);
}
CODE FRAGMENT B:
public static void main(String args[]) {
try { System.out.println(args[args.length]); }
catch (ArrayIndexOutOfBoundsException e) {}
}
CODE FRAGMENT C:
public static void main(String args[]) {
int ix = args.length;
String last = args[ix];
if (ix != 0) System.out.println(last);
}
CODE FRAGMENT D:
public static void main(String args[]) {
int ix = args.length-1;
if (ix > 0) System.out.println(args[ix]);
}
CODE FRAGMENT E:
public static void main(String args[]) {
try { System.out.println(args[args.length-1]); }
catch (NullPointerException e) {}
3. What will be written to the standard output when the following program is run?
class Base {
int i;
Base() {
add(1);
}
void add(int v) {
i += v;
}
void print() {
System.out.println(i);
}
}
class Extension extends Base {
Extension() {
add(2);
}
void add(int v) {
i += v*2;
}
}
public class Qd073 {
public static void main(String args[]) {
bogo(new Extension());
}
static void bogo(Base b) {
b.add(8);
b.print();
}
}
A]9
B]18
C]20
D]21
E]22
4. interface I {
void setValue(int val);
int getValue();
}
DEFINITION A:
(a)class A extends I {
int value;
void setValue(int val) { value = val; }
int getValue() { return value; }
}
DEFINITION B:
(b)interface B extends I {
void increment();
}
DEFINITION C:
(c)abstract class C implements I {
int getValue() { return 0; }
abstract void increment();
}
DEFINITION D:
(d)interface D implements I {
void increment();
}
DEFINITION E:
(e)class E implements I {
int value;
public void setValue(int val) { value = val; }
}
5. Given the following code, which method declarations, when inserted at the indicated position, will not cause the program to fail compilation?
public class Qdd1f {
public long sum(long a, long b) { return a + b; }
// insert new method declaration here
}
A]public int sum(int a, int b) {return a + b;}
B]public int sum(long a, long b) { return 0;}
C]abstract int sum();
D]private long sum(long a , long b) { return a + b; }
E]public long sum(long a, int b) {return a + b; }
6. What will be written to the standard output when the following program is run?
public class Qcb90 {
int a;
int b;
public void f() {
a = 0;
b = 0;
int[] c = { 0 };
g(b, c);
System.out.println(a + " " + b + " " + c[0] + " ");
}
public void g(int b, int[] c) {
a = 1;
b = 1;
c[0] = 1;
}
public static void main(String args[]) {
Qcb90 obj = new Qcb90();
obj.f();
}
}
A]000
B]001
C]010
D]100
E]101