Java Questions
Java Questions
code or variable definitions. This information will be provided in a formatted section just
above the question.
Q1)
Sample Code import java.awt.*;
class A extends Frame
implements ActionListener, WindowListener
{
public A()
{
Button button1 = new Button("Click Me");
button.addActionListener(this);
add(button1);
show();
}
public actionPerformed(ActionEvent e) {}
void b1() { return; }
}
Referring to the above, what statement should be added to actionPerformed() so that b1()
is invoked for a click on button1?
Q2)
Each method represents a stage in the life of a java applet EXCEPT which one of the
following?
(a) stop()
(b) show()
(c) destroy()
(d) init()
(e) start()
Q3)
Sample Code public double SquareRoot( double value )
throws ArithmeticException
{
if (value >= 0) return Math.sqrt( value );
else throw new ArithmeticException();
}
Referring to the above, what value is returned when method func(4) is invoked?
(a) -2
(b) -1
(c) 0
(d) 1
(e) 2
Q4)
Sample Code int count = 0;
while (count < 12) {
count++;
}
(a) List
(b) PullMenu
(c) Select
(d) Choice
(e) Menu
Q6)
Sample Code SecurityManager sm = System.getSecurityManager();
if(sm != null) {
sm.checkRead("filename.txt");
}
When an untrusted application executes the code above, what happens if the
SecurityManager object is set up to deny this resource?
(a) The SecurityManager will stop the read when read method calls are invoked.
(b) checkRead() will return false
(c) The SecurityManager will cause the java virtual machine to exit.
(d) The SecurityManager will terminate the thread attempting to read the file.
(e) checkRead() will throw a java.security.SecurityException
Q7)
Sample Code int count = 0;
for(int i = 0,j = 0; i<4; i++,j++)
count += j;
(a) 0
(b) 1
(c) 3
(d) 4
(e) 6
Q8)
Sample Code import java.sql.*;
class query {
public static void main(String args[]) {
String str = "Select lname,salary from employee" +
" order by salary desc";
Connection c;
// Insert database initialization code here
Statement s = c.createStatement();
ResultSet rs = s.executeQuery(str);
}
}
Which of the code segments below would initialize the database connection in the above
code?
(b)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = new Connection("sun.jdbc.odbc.JdbcOdbcDriver","Fred");
c.login( "ellen", "password" );
(c)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Fred";
c = DriverManager.getConnection(url, "ellen", "password");
(d)c = DriverManager.getConnection(
"sun.jdbc.odbc.JdbcOdbcDriver", "Fred");
(e)c = DriverManager.getConnection(
"sun.jdbc.odbc.JdbcOdbcDriver", "Fred");
c.login( "ellen", "password");
Q9)
Sample Code int i = 0;
double value = 1.2;
String str = new String("1.2");
Boolean flag = true;
Referring to the above, if a program instantiates a B object and invokes getIt() with a
parameter of 3, what value is returned?
(a) 0
(b) 10
(c) 17
(d) null
(e) The compiler will error because of duplicate method names
Q11)
Which statement expresses a value that CANNOT be modified?
Q12)
Sample Code public interface A {
static final int myCount = 10;
abstract public void method1(int i);
abstract public int method2(float f);
}
-- New File --
class B implements A {
Q14)
Which one of the following declarations is NOT valid?
Q15)
Sample Code class A {
int i = 5;
public static void main(String args[]) {
A h = new A();
h.doIt();
}
void doIt() {
i += 1;
}
}
Referring to the above, which one of the following describes the method main()?
(a) 0
(b) 1.0
(c) 2.0
(d) 2.33
(e) 7.0
Q17)
Sample Code class A {
private int getIt(int i) {
return i*4;
}
}
Referring to the above, what classes can access method getIt() in class A?
Q18)
What is Java serialization?
Referring to the above, following execution, the value of variable time will be the number
of milliseconds since which one of the following?
(a) Midnight
(b) The most recent whole minute
(c) Midnight GMT on January 1, 1988
(d) Midnight GMT on January 1, 1970
(e) The most recent even hour
Q20)
Sample Code int count=0, i=0;
do {
count += i;
i++;
if(count > 5) break;
} while(i<=4);
(a) 0
(b) 1
(c) 4
(d) 6
(e) 10
Q21)
Sample Code class A {
int x = 2, y = 4, z = 7;
int calcW() { return x * y; }
class B {
int w = 9;
public int cvert(int i) { return calcW() * w; }
}
}
(a) 0
(b) 1
(c) 9
(d) 10
(e) 11
Q23)
Sample Code package mypackage;
class MyException extends java.lang.Exception
{
public MyException() { super(); }
public MyException( String s ) { super(s); }
}
Referring to the above, which of the following code segments properly generates an
exception of type "MyException"?
Q24)
Sample Code if (check4Biz(storeNum) < 10) {}
(a) int
(b) Boolean
(c) char[]
(d) String
(e) java.util.Bitset
Q25)
Sample Code int x=3, y=5, z=2;
if(x <= y) {
x += z;
if(z != x)
y = (x - z)/y;
z++;
}
else if (y == 0) {
y++;
z *= y;
}
else y = 10;
Referring to the above, what are the values of x, y, and z after executing the sample code?
Q26)
What type of variable can be changed at runtime but is always the same for all objects of
a given class?
(a) Final
(b) Static
(c) Public protected
(d) Char
(e) Int
Q27)
What class creates a listening TCP connection?
(a) java.net.ServerSocket
(b) java.net.DatagramSocket
(c) java.net.InputSocket
(d) java.net.ListeningSocket
(e) java.net.URLConnection
Q28)
Sample Code float f = 5f;
float g = 2f;
float h;
h = 3+f/g+2;
(a) 2
(b) 4.2
(c) 6
(d) 7
(e) 7.5
Q29)
Sample Code class Counter {
int count = 0;
int increment() {
int n = count;
count = n + 1;
return n;
}
}
Referring to the above, which of the following actions would make increment() thread-
safe?
Q31)
Sample Code import java.sql.*;
// Insert additional code here
Connection con = DriverManager.getConnection(url);
String s = "call proc(?,?,?,?)";
CallableStatement cs = con.prepareCall(s);
cs.setInt(1,1);
cs.setInt(2,2);
cs.setInt(3,3);
cs.registerOutParameter(4,java.sql.TYPES.INTEGER);
Referring to the above, which of the following commands executes procedure proc(),
which includes two INSERT SQL statements and one OUT parameter?
(a) cs.executeQuery()
(b) con.getConnection(cs)
(c) cs.prepareCall().execute()
(d) ResultSet = cs.runQuery()
(e) cs.execute()
Q32)
Applet MyApplet includes custom class Class2. When the browser loads MyApplet.class
and CANNOT find Class2 locally, the browser will do which one of the following?
Q33)
Which class CANNOT be directly instantiated?
(a) java.io.BufferedReader
(b) java.io.DataInputStream
(c) java.io.FileReader
(d) java.io.InputStream
(e) java.io.StringBufferInputStream
Q34)
Which one of the following about a "persistent" object is true?
(a) 0
(b) 1
(c) 2
(d) 3
(e) 6
Q36)
Sample Code int x = 0, y = 0, z = 0;
x = 1;
x = (-x + y++) * ++z;
(a) -2
(b) -1
(c) 0
(d) 1
(e) 2
Q37)
Sample Code class B extends A {
int xC, yC, k;
void move(int x) {
xC = x;
}
}
Q38)
Sample Code public abstract class A
extends B, C
implements D, E { /*...*/ }