Professional Documents
Culture Documents
Question 1
class EBH019 {
public static void main (String args[]) {
int i1 = 0xffffffff, i2 = i1 << 1;
int i3 = i1 >> 1, i4 = i1 >>> 1;
System.out.print(Integer.toHexString(i2) + ",");
System.out.print(Integer.toHexString(i3) + ",");
System.out.print(Integer.toHexString(i4));
}}
class EBH201 {
public static void main (String[] args) {
int a = 1 || 2 ^ 3 && 5;
int b = ((1 || 2) ^ 3) && 5;
int c = 1 || (2 ^ (3 && 5));
System.out.print(a + "," + b + "," + c);
}}
class EBH202 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}}
class EBH203 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = a || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}}
class EBH011 {
public static void main (String[] args) {
float a = Float.POSITIVE_INFINITY;
double b = Double.POSITIVE_INFINITY;
double c = Double.NaN;
System.out.print((a == b)+","+(c == c)+","+(c != c));
}}
class EBH012 {
public static void main (String[] args) {
byte x = 3, y = 5;
System.out.print((-x == ~x + 1)+","+(-y == ~y + 1));
}}
What is the result of attempting to compile and run the program?
a. Prints: false,false
b. Prints: false,true
c. Prints: true,false
d. Prints: true,true
e. Run-time error
f. Compile-time error
g. None of the above
Question 7
class EBH013 {
public static void main (String[] args) {
byte x = 3, y = 5;
System.out.print((~x == -x - 1)+","+(~y == -y - 1));
}}
class EBH014 {
public static void main (String[] args) {
byte x = 3, y = 5;
System.out.print((y % x) + ",");
System.out.print(y == ((y/x)*x + (y%x)));
}}
class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
public static void main (String[] args) {
Color color1 = new Red(); Red color2 = new Red();
boolean b1 = color1 instanceof Color;
boolean b2 = color1 instanceof Blue;
boolean b3 = color2 instanceof Blue;
System.out.print(b1+","+b2+","+b3);
}}
class EBH020 {
public static void main (String[] args) {
int a = 1 | 2 ^ 3 & 5;
int b = ((1 | 2) ^ 3) & 5;
int c = 1 | (2 ^ (3 & 5));
System.out.print(a + "," + b + "," + c);
}}
class EBH025 {
public static void main (String args[]) {
int i1 = 0xffffffff, i2 = i1 << 33;
int i3 = i1 << (33 & 0x1f);
System.out.print(Integer.toHexString(i2) + ",");
System.out.print(Integer.toHexString(i3));
}}
class EBH001 {
static int m(int i) {System.out.print(i + ", "); return i;}
public static void main(String s[]) {
m(m(1) - m(2) + m(3) * m(4));
}}
What is the result of attempting to compile and run the program?
a. Prints: 1, 2, 3, 4, 8,
b. Prints: 1, 2, 3, 4, 11,
c. Prints: 3, 4, 1, 2, 11,
d. Run-time error
e. Compile-time error
f. None of the above
Question 13
class EBH002 {
static int m(int i) {System.out.print(i + ", "); return i;}
public static void main(String s[]) {
m(m(1) + m(2) % m(3) * m(4));
}}
class EBH005 {
public static void main (String[] s) {
byte b = 127; b <<= 2;System.out.println(b);
}}
class EBH007{
public static void main (String[] s) {
byte b = 5; System.out.println(b<<33);
}}
class EBH015 {
public static void main (String[] args) {
System.out.print((new Object() instanceof Object)+",");
System.out.print((new Object() instanceof String)+",");
System.out.print((new String() instanceof Object));
}}
class EBH101 {
static int m(int i) {System.out.print(i + ", "); return i;}
public static void main(String s[]) {
int i = 1; m(m(++i) + m(i++) + m(-i) + m(i++));
}}
What is the result of attempting to compile and run the above program?
a. Prints: 1, 2, 3, 4, 10,
b. Prints: 1, 2, -3, 4, 4,
c. Prints: 2, 2, -3, -3, -2,
d. Prints: 2, 2, -3, 3, 4,
e. Prints: 2, 3, -3, -2, 0,
f. Prints: 2, 3, -3, 4, 6,
g. Prints: 2, 3, 4, 5, 14,
h. Run-time error
i. Compile-time error
j. None of the above
Question 18
class EBH102 {
static int m(int i) {System.out.print(i + ","); return i;}
public static void main(String s[]) {
int i = 1, j = m(i++) + m(i++) * m(i++) + m(i++);
System.out.print(j % 5);
}}
What is the result of attempting to compile and run the above program?
a. Prints: 1,2,3,4,0
b. Prints: 1,2,3,4,1
c. Prints: 1,2,3,4,2
d. Prints: 1,2,3,4,3
e. Prints: 1,2,3,4,4
f. Prints: 1,2,3,4,5
g. Run-time error
h. Compile-time error
i. None of the above
Question 19
class EBH103 {
public static void main (String[] args) {
byte a = 1, b = 2, c = (byte)a++, d = (byte)++b, e = (byte)a + b;
System.out.print(c + d + e);
}}
What is the result of attempting to compile and run the above program?
a. Prints: 1 2 3
b. Prints: 6
c. Prints: 2 3 5
d. Prints: 10
e. Prints: 1 3 4
f. Prints: 8
g. Prints: 1 3 5
h. Prints: 9
i. Run-time error.
j. Compile-time error.
k. None of the above
Question 20
class EBH204 {
static boolean m1(String s, boolean b) {
System.out.print(s + (b ? "T" : "F"));
return b;
}
public static void main(String[] args) {
m1("A",m1("B",false) || m1("C",true) || m1("D",false));
}}
class EBH104 {
static int m(int i) {System.out.print(i + ", "); return i;}
public static void main(String s[]) {
int i = 1; m(m(++i) - m(i++) + m(-i) * m(~i));
}}
What is the result of attempting to compile and run the above program?
a. Prints: 2, 2, -3, -4, 8,
b. Prints: 2, 2, -3, -4, 12,
c. Prints: 2, 3, -3, -4, 7,
d. Prints: 1, 1, 1, 1, 0,
e. Prints: 2, 2, -2, -2, 4,
f. Prints: 2, 3, -2, -2, 3,
g. Prints: -1, -2, 2, 2, 0,
h. Run-time error
i. Compile-time error
j. None of the above
Question 22
class EBH105 {
static int m(int i) {System.out.print(i + ","); return 0;}
public static void main (String[] args) {
int i = 0; i = i++ + m(i); System.out.print(i);
}}
What is the result of attempting to compile and run the above program?
a. Prints: 0,0
b. Prints: 1,0
c. Prints: 0,1
d. Prints: 1,1
e. Run-time error
f. Compile-time error
g. None of the above
Question 23
class EBH106 {
public static void main(String args[]) {
int a = 1; a += ++a + a++; System.out.print(a);
}}
What is the result of attempting to compile and run the above program?
a. Prints: 3
b. Prints: 4
c. Prints: 5
d. Prints: 6
e. Prints: 7
f. Run-time error
g. Compile-time error
h. None of the above
Question 24
class EBH107 {
static int m(int i) {System.out.print(i + ","); return i;}
public static void main(String s[]) {
int i=0, j = m(++i) + m(++i) * m(++i) % m(++i) + m(++i);
System.out.print(j%5);
}}
What is the result of attempting to compile and run the above program?
a. Prints: 1,2,3,4,5,0
b. Prints: 1,2,3,4,5,1
c. Prints: 1,2,3,4,5,2
d. Prints: 1,2,3,4,5,3
e. Prints: 1,2,3,4,5,4
f. Prints: 1,2,3,4,5,5
g. Run-time error
h. Compile-time error
i. None of the above
Question 25
class EBH108 {
public static void main(String s[]) {
int i=0, j = ++i + ((++i * ++i) % ++i) + ++i;
System.out.print(j%5);
}}
What is the result of attempting to compile and run the above program?
a. Prints: 1
b. Prints: 2
c. Prints: 3
d. Prints: 4
e. Prints: 5
f. Run-time error
g. Compile-time error
h. None of the above
Question 26
class EBH023 {
static String m1(boolean b){return b?"T":"F";}
public static void main(String [] args) {
boolean b1 = false?false:true?false:true?false:true;
boolean b2 = false?false:(true?false:(true?false:true));
boolean b3 = ((false?false:true)?false:true)?false:true;
System.out.println(m1(b1) + m1(b2) + m1(b3));
}}
class EBH024 {
public static void main(String[] args) {
int i1 = 15;
String b1 = (i1>30)?"Red":(i1>20)?"Green":(i1>10)?"Blue":"Violet";
String b2 = (i1>30)?"Red":((i1>20)?"Green":((i1>10)?"Blue":"Violet"));
System.out.println(b1 + "," + b2);
}}
class EBH003 {
static int m(int i) {System.out.print(i + ", "); return i;}
public static void main(String s[]) {
m(m(~1) + m(1|2) + m(1&2) + m(1^3) + m(1<<1));
}}
class EBH021 {
public static void main(String[] args) {
System.out.print((-1 & 0x1f) + "," + (8 << -1));
}}
Arguments
Question 1
class GFC401 {
static int m1(int x) {return ++x;}
public static void main (String[] args) {
int x = 1;
int y = m1(x);
System.out.println(x + "," + y);
}}
class GRC10 {
public static void main (String[] s) {
System.out.print(s[1] + s[2] + s[3]);
}}
java GRC10 A B C D E F
What is the result of attempting to compile and run the program using the specified
command line?
a. Prints: ABC
b. Prints: BCD
c. Prints: CDE
d. Prints: A B C
e. Prints: B C D
f. Prints: C D E
g. Compile-time error
h. Run-time error
i. None of the above
Question 3
class GFC402 {
static int x=1;
void m1(int i) {x++; i++;}
public static void main (String[] args) {
int y=3; m1(y);
System.out.println(x + "," + y);
}}
class GFC403 {
private static int x=1;
static void m1(int i) {x++; i++;}
public static void main (String[] args) {
int y=3; m1(y);
System.out.println(x + "," + y);
}}
class GFC404 {
private static int x=1;
static void m1(int x, int y) {x++; y++;}
public static void main (String[] args) {
int y=3; m1(x, y);
System.out.println(x + "," + y);
}}
class GFC301 {
private String name;
public GFC301(String name) {this.name = name;}
public void setName(String name) {this.name = name;}
public String getName() {return name;}
public static void m1(GFC301 r1, GFC301 r2) {
r1.setName("Bird");
r2 = r1;
}
public static void main (String[] args) {
GFC301 pet1 = new GFC301("Dog");
GFC301 pet2 = new GFC301("Cat");
m1(pet1,pet2);
System.out.println(pet1.getName() + "," + pet2.getName());
}}
class GFC303 {
private String name;
public GFC303(String name) {this.name = name;}
public void setName(String name) {this.name = name;}
public String getName() {return name;}
public static void m1(GFC303 pet1, GFC303 pet2) {
pet1 = new GFC303("Fish");
pet2 = null;
}
public static void main (String[] args) {
GFC303 pet1 = new GFC303("Dog");
GFC303 pet2 = new GFC303("Cat");
m1(pet1,pet2);
System.out.println(pet1.getName() + "," + pet2.getName());
}}
class GFC304 {
static void m1(int[] i1, int[] i2) {
int[] i3 = i1; i1 = i2; i2 = i3;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}
class GFC305 {
static void m1(int[] i1, int[] i2) {
int i = i1[0]; i1[0] = i2[0]; i2[0] = i;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}
class GFC306 {
static int[] i1 = {1}, i2 = {3};
static void m1(int[] i1) {
int[] i3 = i1; i1 = i2; i2 = i3;
}
public static void main (String[] args) {
m1(i1);
System.out.print(i1[0] + "," + i2[0]);
}}
class GFC307 {
static void m1(int[] i1, int[] i2) {
i1 = i2 = null;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}
class GFC308 {
int[] i1 = {1}, i2 = {3};
void m1() {
m2(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}
void m2(int[] i1, int[] i2) {
int[] i3 = i1;
this.i1 = i2;
this.i2 = i3;
}
public static void main (String[] args) {
new GFC308().m1();
}}
Exception Handling
Question 1
class A {
public static void main (String[] args) {
Error error = new Error();
Exception exception = new Exception();
System.out.print((exception instanceof Throwable) + ",");
System.out.print(error instanceof Throwable);
}}
class A {
public static void main (String[] args) {
Object error = new Error();
Object runtimeException = new RuntimeException();
System.out.print((error instanceof Exception) + ",");
System.out.print(runtimeException instanceof Exception);
}}
Interfaces
Question 1
interface A {
void m1(); // 1
public void m2(); // 2
protected void m3(); // 3
private void m4(); // 4
}
Which of the following are modifiers that can be applied to an interface that is a
member of a directly enclosing interface?
a. abstract
b. implements
c. final
d. private
e. protected
f. public
Question 5
Which of the following are modifiers that can be applied to an interface that is a
member of a directly enclosing class?
a. abstract
b. extends
c. final
d. private
e. protected
f. public
Question 6
Which of the following are modifiers that can be applied to a field declaration
within an interface?
a. abstract
b. const
c. final
d. private
e. protected
f. public
Question 10
Which of the following modifiers can be applied to a class that is declared within
an enclosing interface?
a. public
b. protected
c. private
d. abstract
e. static
f. final
Question 12
interface A {
int a = 1; // 1
public int b = 2; // 2
public static int c = 3; // 3
public static final int d = 4; // 4
}
interface A {
protected int e = 5; // 1
private int f = 6; // 2
volatile int g = 7; // 3
transient int h = 8; // 4
public static final int d = 9; // 5
}
Which of the field declarations does not result in a compile-time error?
a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 14
Which of the following are modifiers that can be applied to a method declaration
within an interface?
a. abstract
b. final
c. private
d. protected
e. public
Question 15
interface A {
void m1(); // 1
public void m2(); // 2
protected void m3(); // 3
private void m4(); // 4
abstract void m5(); // 5
}
interface A {
final void m1(); // 1
synchronized void m2(); // 2
native void m3(); // 3
abstract void m4(); // 4
public void m5(); // 5
}
Inheritance
Question 1
class C {
void printS1() {System.out.print("C.printS1 ");}
static void printS2() {System.out.print("C.printS2 ");}
}
class D extends C {
void printS1(){System.out.print("D.printS1 ");}
void printS2() {System.out.print("D.printS2 ");}
public static void main (String args[]) {
C c = new D(); c.printS1(); c.printS2();
}}
class E {
void printS1(){System.out.print("E.printS1 ");}
static void printS2() {System.out.print("E.printS2");}
}
class F extends E {
void printS1(){System.out.print("F.printS1 ");}
static void printS2() {System.out.print("F.printS2");}
public static void main (String args[]) {
E x = new F(); x.printS1(); x.printS2();
}}
class P {
static void printS1(){System.out.print("P.printS1 ");}
void printS2() {System.out.print("P.printS2 ");}
void printS1S2(){printS1();printS2();}
}
class Q extends P {
static void printS1(){System.out.print("Q.printS1 ");}
void printS2(){System.out.print("Q.printS2 ");}
public static void main(String[] args) {
new Q().printS1S2();
}}
class R {
private void printS1(){System.out.print("R.printS1 ");}
protected void printS2() {System.out.print("R.printS2 ");}
protected void printS1S2(){printS1();printS2();}
}
class S extends R {
private void printS1(){System.out.print("S.printS1 ");}
protected void printS2(){System.out.print("S.printS2 ");}
public static void main(String[] args) {
new S().printS1S2();
}}
class T {
private int i1, i2;
void printI1I2() {System.out.print("T, i1="+i1+", i2="+i2);}
T(int i1, int i2) {this.i1=i1; this.i2=i2;}
}
class U extends T {
private int i1, i2;
void printI1I2() {System.out.print("U, i1="+i1+", i2="+i2);}
U(int i1, int i2) {this.i1=i1; this.i2=i2;}
public static void main(String[] args) {
T t = new U(1,2); t.printI1I2();
}}
class Leg{}
class Fur{}
abstract class Pet {
public abstract void eat();
public abstract void sleep();
}
class Dog extends Pet {
Leg leftFront = new Leg(), rightFront = new Leg();
Leg leftRear = new Leg(), rightRear = new Leg();
Fur fur = new Fur();
public Fur shed() {return fur;}
public void eat() {}
public void sleep() {}
}
class Cat extends Dog {
public void ignoreOwner() {}
public void climbTree() {}
}
class A {
A() {System.out.print("CA ");}
static {System.out.print("SA ");}
}
class B extends A {
B() {System.out.print("CB ");}
static {System.out.print("SB ");}
public static void main (String[] args) {B b = new B();}
}
What is the result of attempting to compile and run the above program?
a. Prints: SA CA SB CB
b. Prints: SA SB CA CB
c. Prints: SB SA CA CB
d. Prints: SB CB SA CA
e. Runtime Exception
f. Compiler Error
g. None of the above
Question 18
Method Overloading
Question 1
class GFC215 {
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
int a1 = 1; long b1 = 2; System.out.print(m(a1)+","+ m(b1));
}}
class GFC216 {
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
char a1 = 1; long b1 = 2; System.out.print(m(a1)+","+ m(b1));
}}
class GFC217 {
static String m(int i) {return "int";}
static String m(float i) {return "float";}
public static void main (String[] args) {
long a1 = 1; double b1 = 2; System.out.print(m(a1)+","+ m(b1));
}}
class GFC218 {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
public static void main(String[] args) {m(null);}
}
class GFC200 {}
class GFC201 {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
static void m(GFC200 x) {System.out.print("GFC200");}
public static void main(String[] args) {m(null);}
}
class GFC214 {
static void m1(boolean b1) {System.out.print("boolean ");}
static void m1(byte b1) {System.out.print("byte ");}
static void m1(short s1) {System.out.print("short ");}
static void m1(char c1) {System.out.print("char ");}
static void m1(int i1) {System.out.print("int ");}
public static void main(String[] args) {
byte b1; m1(b1 = 1); m1(b1); m1(b1 == 1);
}}
Encapsulation
Question 1
A class can not be called "tightly encapsulated" unless which of the following is
true?
a. The class is declared final.
b. All local variables are declared private.
c. All method parameters are declared final.
d. No method returns a reference to any object that is referenced by an internal
data member.
e. None of the above
Question 4
A class can not be called "tightly encapsulated" unless which of the following is
true?
a. All of the methods are declared private.
b. All of the methods are synchronized.
c. All local variables are declared final.
d. The class is a direct subclass of Object.
e. Accessor methods are used to prevent fields from being set with invalid data.
f. None of the above
Question 5
A class can not be called "tightly encapsulated" unless which of the following are
true?
a. The data members can not be directly manipulated by external code.
b. The class is declared final.
c. It has no public mutator methods.
d. The superclass is tightly encapsulated.
Question 6
A class can not be called "tightly encapsulated" unless which of the following is
true?
a. The class is a nested class.
b. The constructors are declared private.
c. The mutator methods are declared private.
d. The class implements the Encapsulated interface.
e. None of the above
Question 7
A class can not be called "tightly encapsulated" unless which of the following is
true?
a. All member fields are declared final.
b. The class is not anonymous.
c. The internal data model can be read and modified only through accessor and
mutator methods.
d. The class is an inner class.
e. None of the above
Question 8
Threads
Question 1
Which of the following methods are static members of the Thread class?
a. join
b. notify
c. notifyAll
d. run
e. sleep
f. start
g. yield
h. wait
Question 3
Which of the following methods are deprecated members of the Thread class?
a. join
b. notify
c. notifyAll
d. resume
e. run
f. sleep
g. start
h. stop
i. suspend
j. yield
k. wait
Question 4
Which of the following methods name the InterruptedException in its throws clause?
a. join
b. notify
c. notifyAll
d. run
e. sleep
f. start
g. yield
h. wait
Question 5
Which of the following instance methods should only be called by a thread that
holds the lock of the instance on which the method is invoked?
a. join
b. notify
c. notifyAll
d. run
e. start
f. wait
Question 7
The synchronized statement has the form shown above. Which of the following are
true statements?
a. A compile-time error occurs if the expression produces a value of any
reference type
b. A compile-time error occurs if the expression produces a value of any
primitive type
c. A compile-time error does not occur if the expression is of type boolean
d. The sychronized block may be processed normally if the expression is null
e. If execution of the block completes normally, then the lock is released
f. If execution of the block completes abruptly, then the lock is released
g. A thread can hold more than one lock at a time
h. Synchronized statements can be nested
i. Synchronized statements with identical expressions can be nested
Question 10
Which of the following thread state transitions model the lifecycle of a thread?
a. The Dead state to the Ready state
b. The Ready state to the Not-Runnable state
c. The Ready state to the Running state
d. The Running state to the Not-Runnable state
e. The Running state to the Ready state
f. The Not-Runnable state to the Ready state
g. The Not-Runnable state to the Running state
Question 12
Which of the following will not force a thread to move into the Not-Runnable state?
a. Thread.yield method
b. Thread.sleep method
c. Thread.join method
d. Object.wait method
e. By blocking on I/O
f. Unsuccessfully attempting to acquire the lock of an object
g. None of the above
Question 14
Collection
Question 1
import java.util.*;
class GFC100 {
public static void main (String args[]) {
Object a1 = new LinkedList(), b1 = new TreeSet();
Object c1 = new TreeMap();
System.out.print((a1 instanceof Collection)+",");
System.out.print((b1 instanceof Collection)+",");
System.out.print(c1 instanceof Collection);
}}
import java.util.*;
class GFC101 {
public static void main (String args[]) {
Object a1 = new HashMap(), b1 = new ArrayList();
Object c1 = new HashSet();
System.out.print((a1 instanceof Collection)+",");
System.out.print((b1 instanceof Collection)+",");
System.out.print(c1 instanceof Collection);
}}
import java.util.*;
class GFC102 {
public static void main (String args[]) {
Object a = new HashSet();
System.out.print((a instanceof Set)+",");
System.out.print(a instanceof SortedSet);
}}
Which implementation of the List interface provides for the fastest insertion of a
new element into the middle of the list?
a. Vector
b. ArrayList
c. LinkedList
d. None of the above
Question 8
import java.util.*;
class GFC103 {
public static void main (String args[]) {
Object a1 = new TreeSet();
System.out.print((a1 instanceof Set)+",");
System.out.print(a1 instanceof SortedSet);
}}
import java.util.*;
class GFC104 {
public static void main (String args[]) {
LinkedList a1 = new LinkedList();
ArrayList b1 = new ArrayList();
Vector c1 = new Vector();
System.out.print((a1 instanceof List)+",");
System.out.print((b1 instanceof List)+",");
System.out.print(c1 instanceof List);
}}
import java.util.*;
class GFC105 {
public static void main (String args[]) {
Object a = new HashSet(), b = new HashMap();
Object c = new Hashtable();
System.out.print((a instanceof Collection)+",");
System.out.print((b instanceof Collection)+",");
System.out.print(c instanceof Collection);
}}
import java.util.*;
class GFC106 {
public static void main (String args[]) {
Object a = new HashSet(), b = new HashMap();
Object c = new Hashtable();
System.out.print((a instanceof Map)+",");
System.out.print((b instanceof Map)+",");
System.out.print(c instanceof Map);
}}
import java.util.*;
class GFC107 {
public static void main (String args[]) {
Object a = new HashSet(), b = new HashMap();
Object c = new Hashtable();
System.out.print((a instanceof Cloneable)+",");
System.out.print((b instanceof Cloneable)+",");
System.out.print(c instanceof Cloneable);
}}
import java.util.*;
import java.io.Serializable;
class GFC108 {
public static void main (String args[]) {
HashMap a = new HashMap();
boolean b1, b2, b3;
b1 = (a instanceof Cloneable) & (a instanceof Serializable);
b2 = a instanceof Map;
b3 = a instanceof Collection;
System.out.print(b1 + "," + b2 + "," + b3);
}}
import java.util.*;
class GFC110 {
public static void main (String[] args) {
Object m = new LinkedHashMap();
System.out.print((m instanceof Collection)+",");
System.out.print((m instanceof Map)+",");
System.out.print(m instanceof List);
}}
import java.util.*;
class GFC109 {
public static void main (String[] args) {
Object v = new Vector();
System.out.print((v instanceof Collections)+",");
System.out.print((v instanceof Arrays)+",");
System.out.print(v instanceof List);
}}
Questions on JSP
A small note here - In the real exam, there will be less questions of type fill in
the blanks, as compared to these set of questions. Also the real exam questions
will probably be more difficult than these questions.
1. Which of the following is legal JSP syntax to print the value of i. Select
the one correct answer
1. <%int i = 1;%>
<%= i; %>
2. <%int i = 1;
i; %>
3. <%int i = 1%>
<%= i %>
4. <%int i = 1;%>
<%= i %>
5. <%int i = 1%>
<%= i; %>
2. A JSP page called test.jsp is passed a parameter name in the URL using
http://localhost/test.jsp?name="John". The test.jsp contains the following code.
5. What gets printed when the following JSP code is invoked in a browser. Select
the one correct answer.
<%= if(Math.random() < 0.5) %>
hello
<%= } else { %>
hi
<%= } %>
1. The browser will print either hello or hi based upon the return value
of random.
2. The string hello will always get printed.
3. The string hi will always get printed.
4. The JSP file will not compile.
6. Which of the following are correct. Select the one correct answer.
1. JSP scriptlets and declarations result in code that is inserted inside
the _jspService method.
2. The JSP statement <%! int x; %> is equivalent to the statement
<jsp:scriptlet>int x;</jsp:scriptlet%>.
3. The following are some of the predefined variables that maybe used in
JSP expression - httpSession, context.
4. To use the character %> inside a scriptlet, you may use %\> instead.
7. What gets printed when the following is compiled. Select the one correct
answer.
<% int y = 0; %>
<% int z = 0; %>
1. 0
2. 1
3. 2
4. 3
5. The program generates compilation error.
8. Which of the following JSP variables are not available within a JSP
expression. Select the one correct answer.
1. out
2. session
3. request
4. response
5. httpsession
6. page
10. A bean with a property color is loaded using the following statement
<jsp:usebean id="fruit" class="Fruit"/>
Which of the following statements may be used to set the of color property of
the bean. Select the one correct answer.
1. <jsp:setColor id="fruit" property="color" value="white"/>
2. <jsp:setColor name="fruit" property="color" value="white"/>
3. <jsp:setValue name="fruit" property="color" value="white"/>
4. <jsp:setProperty name="fruit" property="color" value="white">
5. <jsp:setProperty name="fruit" property="color" value="white"/>
6. <jsp:setProperty id="fruit" property="color" value="white">
11. A bean with a property color is loaded using the following statement
<jsp:usebean id="fruit" class="Fruit"/>
What happens when the following statement is executed. Select the one correct
answer.
<jsp:setProperty name="fruit" property="*"/>
1. This is incorrect syntax of <jsp:setProperty/> and will generate a
compilation error. Either value or param must be defined.
2. All the properties of the fruit bean are initialized to a value of
null.
3. All the properties of the fruit bean are assigned the values of input
parameters of the JSP page that have the same name.
4. All the properties of the fruit bean are initialized to a value of *.
13. Which of the following represents a correct syntax for usebean. Select the
two correct answers.
1. <jsp:usebean id="fruit scope ="page"/>
2. <jsp:usebean id="fruit type ="String"/>
3. <jsp:usebean id="fruit type ="String" beanName="Fruit"/>
4. <jsp:usebean id="fruit class="Fruit" beanName="Fruit"/>
15. Which of the following statements are true for <jsp:usebean>. Select the two
correct answers.
16. Which of these are legal attributes of page directive. Select the two correct
answers.
1. include
2. scope
3. errorPage
4. session
5. debug
17. Which of the following represents the XML equivalent of this statement <%@
include file="a.jsp"%> . Select the one correct statement
1. <jsp:include file="a.jsp"/>
2. <jsp:include page="a.jsp"/>
3. <jsp:directive.include file="a.jsp"/>
4. There is no XML equivalent of include directive.
18. Assume that you need to write a JSP page that adds numbers from one to ten,
and then print the output.
<% int sum = 0;
for(j = 0; j < 10; j++) { %>
// XXX --- Add j to sum
<% } %>
// YYY --- Display ths sum
Which statement when placed at the location XXX can be used to compute the
sum. Select the one correct statement
1. <% sum = sum + j %>
2. <% sum = sum + j; %>
3. <%= sum = sum + j %>
4. <%= sum = sum + j; %>
19. Now consider the same JSP example as last question. What must be added at the
location YYY to print the sum of ten numbers. Select the one correct statement
1. <% sum %>
2. <% sum; %>
3. <%= sum %>
4. <%= sum; %>
20. JSP pages have access to implicit objects that are exposed automatically. One
such object that is available is request. The request object is an instance of
which class?
1. HttpRequest
2. ServletRequest
3. Request
4. HttpServletRequest
21. JSP pages have access to implicit objects that are exposed automatically.
Name the implicit object that is of type HttpSession.
1. session
2. application
3. httpSession
4. httpsession
22. A Java bean with a property color is loaded using the following statement
<jsp:usebean id="fruit" class="Fruit"/>
What is the effect of the following statement.
<jsp:setproperty name="fruit" property="color"/>
Select the one correct answer.
1. An error gets generated because the value attribute of setAttribute is
not defined.
2. The color attribute is assigned a value null.
3. The color attribute is assigned a value "".
4. If there is a non-null request parameter with name color, then its
value gets assigned to color property of Java Bean fruit.
23. The page directive is used to convey information about the page to JSP
container. Which of these are legal syntax of page directive. Select the two
correct statement
1. <% page info="test page" %>
2. <%@ page info="test page" session="false"%>
3. <%@ page session="true" %>
4. <%@ page isErrorPage="errorPage.jsp" %>
5. <%@ page isThreadSafe=true %>
24. Is the following JSP code legal? Select the one correct statement.
<%@page info="test page" session="false"%>
<%@page session="false"%>
25. A JSP page needs to generate an XML file. Which attribute of page directive
may be used to specify that the JSP page is generating an XML file.
1. contentType
2. generateXML
3. type
4. outputXML
26. A JSP page uses the java.util.ArrayList class many times. Instead of
referring the class by its complete package name each time, we want to just use
ArrayList. Which attribute of page directive must be specified to achieve this.
Select the one correct answer.
1. extends
2. import
3. include
4. package
5. classpath
27. Which of these are true. Select the two correct answers.
28. Which of the following are examples of JSP directive. Select the two correct
answers.
1. include
2. exclude
3. import
4. taglibrary
5. servlet
6. page
29. Which of these is true about include directive. Select the one correct
answer.
1. The included file must have jspf extension.
2. The XML syntax of include directive in <jsp:include file="fileName"/> .
3. The content of file included using include directive, cannot refer to
variables local to the original page.
4. When using the include directive, the JSP container treats the file to
be included as if it was part of the original file.
30. Name the implicit variable available to JSP pages that may be used to access
all the other implicit objects.
1. page
2. pageContext
3. context
4. object
5. jspPave
1. d. When using scriptlets (that is code included within <% %>), the included
code must have legal Java syntax. So the first statement must end with a semi-
colon. The second statement on the other hand is a JSP expression. So it must not
end with a semi colon.
2. b. JSP declarations do not have access to automatically defined variables
like request, response etc.
3. b. The XML syntax for JSP expression is <jsp:expression>Java
expression</jsp:expression>
4. e. The XML syntax for JSP scriptlets is <jsp:scriptlet>Java
code</jsp:scriptlet>
5. d. The if statement, else statement and closing parenthesis are JSP
scriptlets and not JSP expressions. So these should be included within <% } %>
6. d. JSP declarations are inserted outside of _jspService method. Hence a is
incorrect. The JSP statement <%!int a;%> is equivalent to <jsp:declaration>int
x;</jsp:declaration>. Hence b is incorrect. The predefined variables that are
available within the JSP expression are session and pageContext, and not
httpSession and context. Hence c is incorrect.
7. c. After the for loop z and y are both set to 3. The else satement gets
evaluated, and 2 gets printed in the browser.
8. e. There is no such variable as httpsession.
9. jsp:getProperty takes two attributes - name and property. The name attribute
must match the id attribute of jsp:usebean.
10. e. The jsp:setProperty takes three attributes - name, property and value.
Also the jsp:setProperty must end with />.
11. c. Using * for property is legal syntax. Bean properties are associated with
identically named input parameters.
12. true. The page directive is defined as <%@page isThreadSafe="false"%>
13. b,c.
14. a. The default scope of the declared bean is page.
15. a,d. The scope and class attributes are not required. But either class or
type must be defined.
16. c,d. The following are legal attributes of page directive - import,
isThreadSafe, session, contentType, autoFlush, extends, info, errorPage,
isErrorPage, language.
17. c. <jsp:directive.include> is the XML equivalent of include directive.
18. b. As this is a a Java statement it needs to be included with <% and %>, and
it needs to end in semi-colon.
19. c. As this is a a Java expression it needs to be included with <%= and %>,
and it should not end in semi-colon.
20. d. request is an instance of HttpServletRequest
21. a. Implicit object session is of type HttpSession.
22. d. This is a legal syntax to set a property of JavaBean. The value attribute
of setProperty is optional.
23. b,c. The option a is incorrect because page directive must be included within
<%@ . d is incorrect because the value of isErrorPage attribute must be boolean. e
is incorrect because, the value true is not within quotes.
24. b. Except the import attribute of page directive, all the other attributes of
page directive cannot be specified more than once. In this example session
attribute is specified twice.
25. a. contentType attribute is used to generate XML. The syntax will look like -
<%@page contentType="text/xml"/>
26. B. The syntax will look like -
<%@page import="java.util.ArrayList"/>
27. a,c. The default value of isThreadSafe attribute is true. So a is correct. If
isThreadSafe is set to false, then JSP container processes request sequentially,
and this leads to poor performance. Hence b and d are incorrect.
28. a,f. include, taglib and page are examples of JSP directives. The JSP
directives have this syntax -
<%@directive attribute="value" %>
29. d. It is not required that the included file has jspf extension. Hence a is
incorrect. The XML syntax of include directive is <jsp:directive.include>
30. b. This pageContext object is an instance of type
javax.servlet.jsp.PageContext, and provides methods like getPage(), getRequest(),
etc. to access other input variables.
Questions on Servlets
1. The method getWriter returns an object of type PrintWriter. This class has
println methods to generate output. Which of these classes define the getWriter
method? Select the one correct answer.
1. HttpServletRequest
2. HttpServletResponse
3. ServletConfig
4. ServletContext
2. Name the method defined in the HttpServletResponse class that may be used to
set the content type. Select the one correct answer.
1. setType
2. setContent
3. setContentType
4. setResponseContentType
3. Which of the following statement is correct. Select the one correct answer.
1. The response from the dedicated server to a HEAD request consists of
status line, content type and the document.
2. The response from the server to a GET request does not contain a
document.
3. The setStatus method defined in the HttpServletRequest class takes an
int as an argument and sets the status of Http response
4. The HttpServletResponse defines constants like SC_NOT_FOUND that may be
used as a parameter to setStatus method.
6. Which of the following statements are correct about the status of the Http
response. Select the one correct answer.
1. A status of 200 to 299 signifies that the request was successful.
2. A status of 300 to 399 are informational messages.
3. A status of 400 to 499 indicates an error in the server.
4. A status of 500 to 599 indicates an error in the client.
1. getStream
2. getOutputStream
3. getBinaryStream
4. getWriter
1. getStream
2. getOutputStream
3. getBinaryStream
4. getWriter
9. Is the following statement true or false. URL rewriting may be used when a
browser is disabled. In URL encoding the session id is included as part of the URL.
10. Name the class that includes the getSession method that is used to get the
HttpSession object.
1. HttpServletRequest
2. HttpServletResponse
3. SessionContext
4. SessionConfig
11. Which of the following are correct statements? Select the two correct
answers.
1. The getRequestDispatcher method of ServletContext class takes the full
path of the servlet, whereas the getRequestDispatcher method of HttpServletRequest
class takes the path of the servlet relative to the ServletContext.
2. The include method defined in the RequestDispatcher class can be used
to access one servlet from another. But it can be invoked only if no output has
been sent to the server.
3. The getRequestDispatcher(String URL) is defined in both ServletContext
and HttpServletRequest method
4. The getNamedDispatcher(String) defined in HttpServletRequest class
takes the name of the servlet and returns an object of RequestDispatcher class.
13. Which HTTP method gets invoked when a user clicks on a link? Select the one
correct answer.
1. GET method
2. POST method
3. HEAD method
4. PUT method
14. When using HTML forms which of the folowing is true for POST method? Select
the one correct answer.
1. POST allows users to bookmark URLs with parameters.
2. The POST method should not be used when large amount of data needs to
be transferred.
3. POST allows secure data transmission over the http method.
4. POST method sends data in the body of the request.
15. Which of the following is not a valid HTTP/1.1 method. Select the one correct
answer.
1. CONNECT method
2. COMPARE method
3. OPTIONS method
4. TRACE method
16. Name the http method used to send resources to the server. Select the one
correct answer.
1. FTP methodd
2. PUT method
3. WRITE method
4. COPY method
17. Name the http method that sends the same response as the request. Select the
one correct answer.
1. DEBUG method
2. TRACE method
3. OPTIONS method
4. HEAD method
18. Which three digit error codes represent an error in request from client?
Select the one correct answer.
1. Codes starting from 200
2. Codes starting from 300
3. Codes starting from 400
4. Codes starting from 500
19. Name the location of compiled class files within a war file? Select the one
correct answer.
1. /META-INF/classes
2. /classes
3. /WEB-INF/classes
4. /root/classes