64 Java Questions For Any Job Interview
64 Java Questions For Any Job Interview
a) MyProg
b) "I"
c) "like"
d) 3
e) 4
a) array
b) boolean
c) Integer
d) protect
e) super
4. After the declaration:
char[] c = new char[100];
what is the value of c[50]?
a) 50
b) 49
c) ‘\u0000′
d) ‘\u0020′
e) " "
f) cannot be determined
a) -231 to 231-1
b) -216 to 216 -
1
c) -232 to 232
d) -216 to 216
a) _xpoints
b) r2d2
c) bBb$
d) set-flow
e) thisisCrazy
7. Represent the number 6 as a hexadecimal
literal.
a) 1000 0001
b) 1000 0000
c) 0000 0001
d) 1001 1101
e) 1001 1100
10. Which statements are accurate:
a) >>
performs signed shift while >>> performs an unsigned shift.
b) >>> performs a
signed shift while >> performs an unsigned shift.
c) << performs a
signed shift while <<< performs an insigned shift.
d) <<< performs a
signed shift while << performs an unsigned shift.
11. The statement …
String s = "Hello" +
"Java";
yields the same value for s as …
String s = "Hello";
String s2= "Java";
s.concat( s2 );
True
False
12. If you compile and execute an application
with the following code in its main() method:
String s = new String( "Computer" );
if( s == "Computer" )
The expression
grade == 70
is evaluated:
a) in both 1 and 2
b) in neither 1 nor 2
c) in 1 but not 2
d) in 2 but not 1
byte myByte;
int myInt;
long myLong;
char myChar;
float myFloat;
double myDouble;
Which one of the following assignments would
need an explicit cast?
a) myInt
= myByte;
b) myInt
= myLong;
c) myByte
= 3;
d) myInt
= myChar;
e) myFloat
= myDouble;
f) myFloat
= 3;
g) myDouble
= 3.0;
15. Consider this class example:
class MyPoint
{ void myMethod()
{ int x, y;
x = 5; y = 3;
System.out.print( " ( " + x + ", " + y + " ) " );
switchCoords( x, y );
System.out.print( " ( " + x + ", " + y + " ) " );
}
void switchCoords( int x, int y )
{ int temp;
temp = x;
x = y;
y = temp;
System.out.print( " ( " + x + ", " + y + " ) " );
}
}
What is printed to standard output if myMethod()
is executed?
a) double
snow[] = new double[31];
b) double
snow[31] = new array[31];
c) double
snow[31] = new array;
d) double[]
snow = new double[31];
17. If arr[] contains only positive integer values, what does this
function do?
public int guessWhat( int arr[] )
{ int x= 0;
for( int i = 0; i < arr.length; i++ )
x = x < arr[i] ? arr[i] : x;
return x;
}
a) arr[n].length();
b) arr.size;
c) arr.size-1;
d) arr[n][size];
e) arr[n].length;
19. If size = 4, triArraylooks like:
int[][] makeArray( int size)
{ int[][] triArray = new int[size] [];
int val=1;
for( int i = 0; i < triArray.length; i++ )
{ triArray[i] = new int[i+1];
for( int j=0; j < triArray[i].length; j++ )
{ triArray[i][j] = val++;
}
}
return triArray;
}
a)
1234
567
89
10
b)
1 4 9 16
c)
1234
d)
1234
5678
9 10 11 12
13 14 15 16
e)
1
23
456
7 8 9 10
d) int[][]a = new[5]int[5];
21. Which of the following are correct methods
for initializing the array "dayhigh" with 7 values?
d) int dayhigh [] = new int[24, 23, 24, 25, 25, 23, 21];
23. If you want a member variable to not be accessible outside the current
class at all, what keyword should precede the name of the variable when
declaring it?
{ int a = 5;
System.out.println( cube( a ) );
What will happen when you attempt to compile and run this code?
int one = 1;
int two = 2;
char initial = ‘2′;
boolean flag = true;
Which of the following are valid?
a) if(
one ){}
b) if(
one = two ){}
c) if(
one == two ){}
d) if(
flag ){}
e) switch(
one ){}
f) switch(
flag ){}
g) switch(
initial ){}
26. If val = 1 in the code below:
switch( val )
{ case 1: System.out.print( "P" );
case 2:
case 3: System.out.print( "Q" );
break;
case 4: System.out.print( "R" );
default: System.out.print( "S" );
}
a) P
b) Q
c) R
d) S
27. Assume that val has been defined as an int for the
code below:
a) val < 0
e) val = 0
m = 0;
while( m++ < 2 )
System.out.println( m );
a) 0
b) 1
c) 2
d) 3
a) i = 1, j = 1
b) i = 1, j = 2
c) i = 1, j = 3
d) i = 2, j = 1
e) i = 2, j = 2
f) i = 2, j = 3
g) i = 3, j = 1
h) i = 3, j = 2
31. Consider the code below:
void myMethod()
{ try
{
fragile();
}
catch( NullPointerException npex )
{
System.out.println( "NullPointerException thrown " );
}
catch( Exception ex )
{
System.out.println( "Exception thrown " );
}
finally
{
System.out.println( "Done with exceptions " );
}
System.out.println( "myMethod is done" );
}
What is printed to standard output if fragile()
throws an IllegalArgumentException?
a) "NullPointerException thrown"
b) "Exception thrown"
d) "myMethod is done"
e) Nothing is printed
32. Consider the following code sample:
class Tree{}
class Pine extends Tree{}
class Oak extends Tree{}
public class Forest
{ public static void main( String[] args )
{ Tree tree = new Pine();
a) Pine
b) Tree
c) Forest
d) Oops
e) (nothing printed)
33. Consider the classes defined below:
import java.io.*;
class Super
{
int methodOne( int a, long b ) throws IOException
{ // code that performs some calculations
}
float methodTwo( char a, int b )
{ // code that performs other calculations
}
}
public class Sub extends Super
{
}
Which of the following are legal method
declarations to add to the class Sub? Assume that each method is the only one being added.
b) float methodTwo(){}
a) 0
b) 1
c) 2
d) 3
e) 4
37. Which of the following statements about
Java’s garbage collection are true?
a) The garbage
collector can be invoked explicitly using a Runtime object.
a) wait()
b) run()
c) stop()
d) update()
e) resume()
40. What class defines the wait() method?
a) equals( s )
b) substring(
s )
c) concat(
s )
d) toUpperCase(
s )
1. a) null
b) "eno"
c) "enoba"
d) "no"
44. What method(s)
from the java.lang.Math class might method() be if the statement
method( -4.4 )
== -4;
is true.
a)
round()
b) min()
c) trunc()
d) abs()
e) floor()
f) ceil()
45. Which methods
does java.lang.Math include for trigonometric computations?
a)
sin()
b) cos()
c) tan()
d) aSin()
e) aCos()
f) aTan()
g) toDegree()
46. This piece of
code:
TextArea ta =
new TextArea( 10, 3 );
Produces (select
all correct statements):
a)
a TextArea with 10 rows and up to 3 columns
b) a TextArea with a
variable number of columns not less than 10 and 3 rows
a)
Panel
b) Dialog
c) Container
d) Frame
48. Of the five Component methods listed below, only one is also a method
of the class MenuItem. Which one?
a)
setVisible(
boolean b )
b) setEnabled( boolean b )
c) getSize()
d) setForeground( Color c )
e) setBackground( Color c )
49. If a font with
variable width is used to construct the string text for a column, the
initial size of the column is:
a)
determined by the number of characters in the string, multiplied by the
width of a character in this font
d) undetermined
a)
fillRect()
b)
drawRect()
c) fillPolygon()
d) drawPolygon()
e) drawLine()
51. The Container methods add( Component comp ) and add( String name, Component comp) will throw an
IllegalArgumentException
if comp is a:
a) button
b) list
c) window
d) textarea
a)
LayoutManager
b) GridBagLayout
c) ActionListener
d) WindowAdapter
e) FlowLayout
53. A component that should resize vertically but not horizontally should
be placed in a:
a)
BorderLayout in the North or South location
c) BorderLayout in the
East or West location
d) BorderLayout in the
Center location
e) GridLayout
54. What type of object is the parameter for all methods of the
MouseListener interface?
55. Which of the following statements about event handling in JDK 1.1 and
later are true?
a)
A class can implement multiple listener interfaces
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">
b.
The string hello will always get
printed.
c.
The string hi will always get printed.
d.
The JSP file will not compile.
...
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<%=
request.getSession(false).getId() %>
</body></html>
1)It won’t
compile
2)It will
print the session id.
3)It will produce a NullPointerException as the
getSession(false) method’s call returns null, cause no session had been
created.
4)It will
produce an empty page.
63. 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
A. <% page info="test page" %>
B. <%@ page info="test page"
session="false"%>
C. <%@ page session="true" %>
D. <%@ page
isErrorPage="errorPage.jsp" %>
E. <%@ page isThreadSafe=true %>
64. Which
of the following are methods of the Cookie Class?
1) setComment
2) getVersion
3) setMaxAge
4) getSecure