Which of the following is correct?
Every Java class, except Object, extends exactly one other class.
Every non-abstract Java class, except Object, extends exactly one
other Java class. An abstract class need not extend any class.
Every Java class extends exactly one other class.
An abstract class must extend another abstract class.
Under special circumstances, a class may extend itself. For example, Object
extends Object.
Consider the code segment
Object s = new String("Hello, world!");
System.out.println(s);
Which of the following is correct about this code segment?
The code will not compile because an object reference of type Object cannot refer to a String object.
The code compiles and, when run, outputs the greeting: Hello, world!.
The code would compile only if the reference to the String object were explicitly cast to type Object.
The code compiles but, at run time, throws an IllegalCastException.
In Java
Every abstract class must implement at least one interface.
No interface can be empty.
An interface may extend none or more interfaces.
An interface may implement another interface.
An interface may extend an abstract class.
Consider the interface Interface_1:
public interface Interface_1 {
public static final int x = -1;
public void no_op() { }
public abstract void print() {
System.out.println("Hello, world!");
}
}
Which of the following best explains why the code will not compile?
All methods in an interface must be explicitly declared abstract and the
method no_op is not so declared.
No method in an interface may have a body.
An interface cannot contain a field such as x in this example.
The code will compile as is.
Consider the class C and its member method m:
package p1;
class C {
public void m() { }
}
Method m cannot be invoked outside of package p1.
Method m can be invoked outside of package p1.
The code will not compile because a non-public class cannot have a public member.
Method m must be declared static in order to be declared public.
Consider the code segment
System.out.println(2 / 3.0);
Which of the following is correct?
The code will not compile because the integer value 2 cannot be divided by the floating-point value 3.0.
The compiler will convert the floating-point value 3.0 to the integer value 3; hence, the output will be 0.
A division involving mixed types is undefined; hence, the output will be either 0 or roughly 0.666666.
The compiler will convert the integer value 2 to the floating-poing value 2.0; hence, the output will be roughly 0.666666.
The interface java.util.Map is implemented by the class java.util.HashMap. Given the statement
java.util.Map map = new java.util.HashMap();
which of the following is correct?
The code will not compile unless the value returned by the new operator is explicitly cast to type Map.
The code will not compile unless the object reference map is declared as type HashMap.
The code compiles as is.
The code will not compile without the appropriate import statements.
The program
class Main {
public static void main(String[ ] args) {
int y;
System.out.println("x + y == " + (x + y));
}
static int x;
}
generates a compiler error because
The field x must be declared above the method main because x is used in main.
The expression x + y must not occur in parentheses within a print statement.
Only field x must be initialized before used.
Local variable y must be initialized before used.
Both field x and local variable y must be initialized before used.
When the program
class Main {
public static void main(String[ ] args) {
int[ ] array = new int[5];
try {
array[5] = 0xfffffffa;
}
catch(Exception e) {
System.out.print("A");
}
finally {
System.out.print("B");
}
System.out.println("C");
}
}
executes, the output is
A
B
C
BC
ABC
Assume that the two classes C and P, shown below, reside in the file P.java.
class C extends P {
double d = 3.1415;
C(double d) { this.d = d; }
}
class P {
int n = -1;
P(int n) { this.n = n; }
}
Which of the following explains why the code will not compile?
P must be declared public for it to be visible to C.
Because C is the subclass, it must be declared below its superclass P.
Every subclass must have a no-argument constructor.
Every subclass constructor must invoke a superclass constructor.
If the invocation is not explicit, then the superclass's no-argument constructor is invoked implicitly by default;
but P does not have a no-argument constructor for the subclass constructor to invoke implicitly.
A field such as n in class P cannot have the same name as a local variable or parameter.
For the program
class Main {
public static void main(String[ ] args) {
int[ ][ ] t = {new int[3], new int[2], new int[3]};
int i = 0, k = 0;
while (true) {
int j = 0;
while (j < t[i].length)
t[i][j++] = k++;
i++;
if (i >= t.length) break;
}
for (int[ ] r : t)
for (int c : r)
System.out.print(c + " ");
System.out.println();
}
}
what is the output?
0 1 2 3 4 5 6 7
0 2 4 6 1 3 4 7
7 6 5 4 3 2 1 0
3 3 3 2 2 2 7 0
For the program
class A {
public void m(String s) { this.print(s); }
protected void print(String s) { System.out.print(s + " "); }
}
class Z extends A {
public void m(String s) {
super.m(s);
print(s);
}
}
class Main {
public static void main(String[ ] args) {
A a1 = new Z();
a1.m("Z");
}
}
what is the output?
Z Z
Z
A Z
Z A
A A
For the code
class Alpha {
int n;
public void m() { this.n = -1; }
public void m(int n) { this.n = n; }
}
class Omega extends Alpha {
int w;
public void m(int k) { this.w = k; }
}
which of the following best describes overloading and overriding?
Method m in Omega overloads method m in Alpha with the same signature.
In Omega the field w must be renamed n for Omega's method m to override Alpha's method m with with same signature.
In these two classes, there is neither overloading nor overriding.
In Alpha, method m is overloaded. Also, method m in Omega overrides the two methods in Alpha.
In Alpha, method m is overloaded and the method in Omega overrides the one-argument method in Alpha.
For the program
class Main {
public static void main(String[ ] args) {
new Main().m();
}
void m() {
String s = "foo";
k(s);
System.out.println(s);
}
void k(String obj_ref) {
obj_ref = "bar";
}
}
what is the output?
bar
foo
foobar
barfoo
foofoo
For the program
class Main {
public static void main(String[] args) {
new Main().p(4);
}
void p(int n) {
if (n <= 0) return;
p(n - 1);
System.out.print(n + " ");
}
}
what is the output?
1 2 3 4
4 3 2 1
4
0
When the program
public class myClass {
private static int sCount = 0;
private int iCount;
public myClass ( ) {
sCount++;
iCount = 0;
}
public void increment() {
sCount++;
iCount++;
}
public String toString() {
return " sCount is " + sCount + " iCount is " + iCount;
}
public static void main( String [] arg ) {
myClass c1,c2;
c1 = new myClass();
c2 = new myClass();
c1.increment();
c1.increment();
System.out.println( c1.toString() );
System.out.println( c2.toString() );
}
}
executes, the output is
sCount is 4 iCount is 2 (newline) sCount is 4 iCount is 0
sCount is 4 iCount is 0 (newline) sCount is 0 iCount is 4
sCount is 4 iCount is 0
sCount is 4 iCount is 4
sCount is 2 iCount is 2 (newline) sCount is 4 iCount is 4
In the code segment
java.awt.Panel my_applet = new java.applet.Applet();
which of the following best describes the data type of my_applet?
(The class java.applet.Applet extends java.awt.Panel.)
The statement will not compile because of bad data types.
The compile-time type of my_applet is Panel, whereas the run-time type is Applet.
The compile-time type of my_applet is Applet, whereas the run-time type is Panel.
The object reference my_applet has no data type.
When the program
class Array {
int v;
public static void main(String[ ] args) {
Array[ ] array_ref = new Array[3];
array_ref[0].v = 10;
System.out.println(array_ref[0].v);
}
}
executes, which of the following describes what happens?
(The class java.applet.Applet extends java.awt.Panel.)
The program throws a NullPointerException.
The program outputs 10 and terminates normally.
The program does not run because it will not compile.
The program throws a FieldNotAccessibleException because of field v.