64 Java questions for any job interview

1. Which
of the following are valid definitions of an application’s main( ) method?
a) public static void main();
b) public static void main( String args );
c) public static void main( String args[] );
d) public static void main( Graphics g );
e) public static boolean main( String args[] );
2. If MyProg.java were compiled as an
application and then run from the command line as:

              java MyProg I like tests

what would be the value of args[ 1 ] inside
the main( ) method?
a) MyProg
b) "I"
c) "like"
d) 3
e) 4
f) null until a value is assigned

3. Which of the following are Java keywords?
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
g) always null until a value is
assigned
5. After the declaration:

int x;

the range of x is:
a) -231 to 231-1

b) -216 to 216 -
1
c) -232 to 232
d) -216 to 216
e) cannot be determined; it depends on
the machine
6. Which identifiers are valid?
a) _xpoints
b) r2d2
c) bBb$
d) set-flow
e) thisisCrazy
7. Represent the number 6 as a hexadecimal
literal.

8. Which of the following statements assigns "Hello Java" to the
String variable s?
a) String s = "Hello Java";
b) String s[] = "Hello Java";
c) new String s = "Hello Java";
d) String s = new String("Hello Java");
9. An integer, x has a binary value (using 1
byte) of 10011100. What is the binary value of z after these statements:

int y = 1 << 7;
int z = x & y;

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" )
               System.out.println( "Equal A" );
        if( s.equals( "Computer" ) )
               System.out.println( "Equal B" );

a) It will not compile because the
String class does not support the = = operator.
b) It will compile and run, but
nothing is printed.
c) "Equal A" is the only
thing that is printed.
d) "Equal B" is the only
thing that is printed.
e) Both "Equal A" and
"Equal B" are printed.
13. Consider the two statements:

        1. boolean passingScore = false && grade == 70;
        2. boolean passingScore = false & grade == 70;

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
e) invalid because false should be
FALSE
14. Given the variable declarations below:

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) (5, 3) (5, 3) (5, 3)
b) (5, 3) (3, 5) (3, 5)
c) (5, 3) (3, 5) (5, 3)
16. To declare an array of 31 floating
point numbers representing snowfall for each day of March in Gnome, Alaska,
which declarations would be valid?
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) Returns the index of the highest
element in the array
b) Returns true/false if there
are any elements that repeat in the array
c) Returns how many even numbers are
in the array
d) Returns the highest element in the
array
e) Returns the number of question
marks in the array
18. Consider the code below:

arr[0] = new int[4];
arr[1] = new int[3];
arr[2] = new int[2];
arr[3] = new int[1];
for( int n = 0; n < 4; n++ )
System.out.println( /* what goes here? */ );

Which statement below, when inserted as the
body of the for loop, would print the number of values in each row?
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)
1 2 3 4
5 6 7
8 9
10

b)
1 4 9 16

c)
1 2 3 4

d)
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

e)
1
2 3
4 5 6
7 8 9 10

20. Which of the following are legal
declarations of a two-dimensional array of integers?
a) int[5][5]a = new int[][];
b) int a = new int[5,5];
c) int[]a[] = new int[5][5];
d) int[][]a = new[5]int[5];
21. Which of the following are correct methods
for initializing the array "dayhigh" with 7 values?
a) int dayhigh = { 24, 23, 24, 25, 25, 23, 21 };
b) int dayhigh[] = { 24, 23, 24, 25, 25, 23, 21 };
c) int[] dayhigh = { 24, 23, 24, 25, 25, 23, 21 };
d) int dayhigh [] = new int[24, 23, 24, 25, 25, 23, 21];
e) int dayhigh = new[24, 23, 24, 25, 25, 23, 21];
22. If you want subclasses to access, but not to
override a superclass member method, what keyword should precede the name of
the superclass method?

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?

24. Consider the code below:

 
public static void main( String args[] )
{  int a = 5;
   System.out.println( cube( a ) );
}
int cube( int theNum )
{
   return theNum * theNum * theNum;
}
 

What will happen when you attempt to compile and run this code?
a) It will not compile because cube is
already defined in the java.lang.Math class.
b) It will not compile because cube is
not static.
c) It will compile, but throw an
arithmetic exception.
d) It will run perfectly and print
"125" to standard output.
25. Given the variables defined below:

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" );
}
 

Which values would be printed?
a) P
b) Q
c) R
d) S
27. Assume that val has been defined as an int for the
code below:

 
if( val > 4 ) 
{  System.out.println( "Test A" );
}
else if( val > 9 ) 
{  System.out.println( "Test B" );
}
else System.out.println( "Test C" );
 

Which values of val will
result in "Test C" being printed:

a) val < 0
b) val between 0 and 4
c) val between 4 and 9
d) val > 9
e) val = 0
f) no values for val will be satisfactory

28. What exception might a wait()
method throw?

29. For the code:

 
m = 0;
while( m++ < 2 )
   System.out.println( m );
 

Which of the following are printed to
standard output?
a) 0
b) 1
c) 2
d) 3
e) Nothing and an exception
is thrown
30. Consider the code fragment below:

 
outer: for( int i = 1; i <3; i++ )
   {  inner: for( j = 1; j < 3; j++ )
      {  if( j==2 )
            continue outer;
            System.out.println( "i = " +i ", j = " + j );
      }
   }    
 

Which of the following would be printed to standard output?
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"
c) "Done with exceptions"
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();
 
      if( tree instanceof Pine )
      System.out.println( "Pine" );
 
      if( tree instanceof Tree )
      System.out.println( "Tree" );
 
      if( tree instanceof Oak )
      System.out.println( "Oak" );
 
      else System.out.println( "Oops" );
   }
}

Select all choices that will be printed:
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.

a) public static void main( String args[] ){}
b) float methodTwo(){}
c) long methodOne( int c, long d ){}
d) int methodOne( int c, long d ) throws
ArithmeticException{}

e) int methodOne( int c, long d ) throws
FileNotFoundException{}

34. Assume that Sub1 and Sub2 are both
subclasses of class Super.
Given the declarations:
Super super = new Super();
Sub1 sub1 = new Sub1();
Sub2 sub2 = new Sub2();
Which statement best describes the result of
attempting to compile and execute the following statement:
super = sub1;
a) Compiles and definitely legal at
runtime
b) Does not compile
c) Compiles and may be illegal at
runtime
35. For the following code:

class Super 
{  int index = 5;
   public void printVal() 
      {  System.out.println( "Super" );
      }
}
class Sub extends Super
{  int index = 2;
   public void printVal() 
   {  System.out.println( "Sub" );
   }
}
public class Runner 
{  public static void main( String argv[] ) 
   {  Super sup = new Sub();
      System.out.print( sup.index + "," );
      sup.printVal();
   }
}

What will be printed to standard output?
a) The code will not compile.
b) The code compiles and "5,
Super" is printed to standard output.
c) The code compiles and "5,
Sub" is printed to standard output.
d) The code compiles and "2,
Super" is printed to standard output.
e) The code compiles and "2,
Sub" is printed to standard output.
f) The code compiles, but throws an
exception.
36. How many objects are eligible for garbage
collection once execution has reached the line labeled Line A?
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
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.
b) The finalize method is
always called before an object is garbage collected.
c) Any class that includes
a finalize method should invoke its superclass’ finalize method.
d) Garbage collection
behavior is very predictable.
38. What line of code would begin execution of a
thread named myThread?

39. Which methods are required to implement the interface Runnable.
a) wait()
b) run()
c) stop()
d) update()
e) resume()
40. What class defines the wait() method?

41. For what reasons might a thread stop execution?
a) A thread with higher priority
began execution.
b) The thread’s wait() method was invoked.
c) The thread invoked its yield() method.
d) The thread’s pause() method was invoked.
e) The thread’s sleep() method was invoked.
42. Which method below can change a String object, s ?
a) equals( s )
b) substring(
s )

c) concat(
s )

d) toUpperCase(
s )

e) none of the above will change s
43. If s1 is declared as:
String s1 =
"phenobarbital";

What will be the value of s2 after
the following line of code:

String s2 = s1.substring( 3, 5 );

  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
    c) a TextArea that may not
    contain more than 30 characters
    d) a TextArea that can be
    edited

    47. In the list below, which subclass(es) of Component cannot be directly
    instantiated:

    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
    b) determined by the number of
    characters in the string, multiplied by the average width of a character
    in this font
    c) exclusively determined by the
    number of characters in the string
    d) undetermined

    50. Which of the
    following methods from the java.awt.Graphics class would be used to draw the outline of a
    rectangle with a single method call?

    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
    e) container that contains
    this container
    52. Of the following AWT classes, which one(s) are responsible for implementing the components layout?

    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
    b) FlowLayout as the first
    component
    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
    b) If a class implements a listener
    interface, it only has to overload the methods it uses
    c) All of the MouseMotionAdapter class
    methods have a void return type.

    56. Which of the
    following describe the sequence of method calls that result in a component
    being redrawn?

    a)
    invoke paint() directly
    b) invoke update which
    calls paint()
    c) invoke repaint() which invokes update(),
    which in turn invokes paint()
    d) invoke repaint() which invokes paint directly

    57. Which of these is a correct fragment within the
    web-app element of deployment descriptor.
    Select the two correct
    answer.

    1. <error-page>
      <error-code>404</error-code>
      <location>/error.jsp</location> </error-page>
    2. <error-page>
      <exception-type>mypackage.MyException</exception-type>
      <error-code>404</error-code>
      <location>/error.jsp</location> </error-page>
    3. <error-page> <exception-type>mypackage.MyException</exception-type>
      <error-code>404</error-code> </error-page>
    4. <error-page>
      <exception-type>mypackage.MyException</exception-type>
      <location>/error.jsp</location> </error-page>

58. 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">

59. 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
<%=
} %>

a.      
The browser will print either hello or
hi based upon the return value of random.

b.     
The string hello will always get
printed.

c.      
The string hi will always get printed.

d.     
The JSP file will not compile.

60. Given the following web application deployment descriptor:

<servlet-class>MyServlet</servlet-class>
...
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>

Which statements are true?

  • 1) servlet-mapping element should be inside servlet element
  • 2) url-pattern can’t be defined that way.
  • 3) if you make the http call: href="http://host/servlet/Hello.do">http://host/Hello.jsp the servlet container will execute MyServlet.

  • 4) It would work with any extension excepting jsp,html,htm

    61.Name the class that includes the getSession method that is used to get the HttpSession object.

      1. HttpServletRequest
      2. HttpServletResponse
      3. SessionContext
      4. SessionConfig

    62. What will be the result of running the following jsp
    file taking into account that the Web server has just been started and this is
    the first page loaded by the server?

    <html><body>

    <%=
    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

      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 %>

    64. Which
    of the following are methods of the Cookie Class?

            1) setComment
            2) getVersion
            3) setMaxAge
            4) getSecure
  • This entry was posted in Java. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

    50 Comments on 64 Java questions for any job interview

    1. Vara rama krishna Chadaram
      Posted 5/10/2006 at 3:59 am | Permalink

      ok

    2. Posted 5/30/2006 at 10:34 am | Permalink

      What will be the output if you try to compile and run this:

      …..
      int i = 9;
      i = ++i;
      i = i++;
      System.out.println(i);
      …..

      Ans. it will print 10, NOT 11 in JAVA.

      to the moderators: it will print 11 in c/c++ but 10 in JAVA. I dont know the reason. If anybody knows, plz mailme.

    3. sekhar
      Posted 6/17/2006 at 1:42 am | Permalink

      java certification exam contains AWT and SWINGS questions??

    4. Gopala Krishna
      Posted 7/4/2006 at 9:53 am | Permalink

      8. Which of the following statements assigns “Hello Java” to the
      String variable s?
      a) String s = “Hello Java”;
      b) String s[] = “Hello Java”;
      c) new String s = “Hello Java”;
      d) String s = new String(”Hello Java”)

      Ans:- a and d

    5. Lucky Chawla
      Posted 7/6/2006 at 10:04 am | Permalink

      Ouestion 1

      c and e

    6. Lucky Chawla
      Posted 7/6/2006 at 10:05 am | Permalink

      Question 2

      ans c

    7. niranjan
      Posted 7/8/2006 at 1:29 pm | Permalink

      pls send me the update interview questions

    8. niranjan
      Posted 7/8/2006 at 1:30 pm | Permalink

      pls send me the update interview questions to my mail id

    9. Amrutha
      Posted 7/11/2006 at 10:59 am | Permalink

      it will be better if you provide answers to all the 64 questions, so that we can check our level.

    10. Amrit
      Posted 7/23/2006 at 11:20 pm | Permalink

      32. all three will be printed
      pine
      tree
      oops

      it’s because pine extends tree class,and else statement will execute because of failure of the if () right before it.

    11. Srinivas Balasani
      Posted 8/4/2006 at 5:20 am | Permalink

      1.Can main mathod be overloaded?

    12. Posted 8/4/2006 at 5:22 am | Permalink

      1.it is better to put all the 64 questions answers in this site.

    13. pape
      Posted 8/7/2006 at 3:50 pm | Permalink

      Questions were thorough.. however only seems to test knowledge of coding and syntax. I think actual “thinking” questions are more helpful in testing a person’s knowledge, especially if the person hasn’t used java in a while. Questions about how something would work, performance, how you would solve a specific problem, etc.
      While questions are plenty, how good is it if we have no answers to check ourselves?

    14. Puneet
      Posted 8/17/2006 at 5:32 am | Permalink

      U can improve the interface as well as provide submit so that one is able to know his/her strong area as well as the weaker one. Also the wrong answers should have proper reason.

    15. Mushtak
      Posted 8/17/2006 at 3:29 pm | Permalink

      Difference between Abstrat and interface?

    16. Surya
      Posted 8/23/2006 at 2:56 am | Permalink
    17. ved prakash
      Posted 9/29/2006 at 3:08 am | Permalink

      hi i think the answer for the question 1 is c where as the others are treated as normal methods and are not invoked directed by the jvm call.

      other options are correct .. but not reflect to the actual main method which can be understood by the jvm

      public static void main(String arg[]) is the syntax

    18. sai
      Posted 10/23/2006 at 4:04 am | Permalink

      for 2 ans is b not c

    19. Alok Srivastava
      Posted 11/1/2006 at 11:00 am | Permalink

      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 + ” ) ” ); }}

      c is the answer..

    20. Posted 11/1/2006 at 11:05 am | Permalink

      public static void main( String args[] ){ int a = 5; System.out.println( cube( a ) );}int cube( int theNum ){ return theNum * theNum * theNum;}

      Ans: c - It won’t compile the code as the reference to non static member is not possible through the static reference.

    21. Alok Srivastava
      Posted 11/1/2006 at 11:10 am | Permalink

      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” );}

      Ans: P and Q will be printed when the code is run.

    22. abul
      Posted 11/10/2006 at 12:05 am | Permalink

      question 11: false. answer will be true if s = s.concat(s2).

    23. abul
      Posted 11/10/2006 at 12:09 am | Permalink

      question 2: i think the ans is c because argument 0 is “I”, argument 1 is “like” and argument 2 is “tests”.

    24. Alok Pandey
      Posted 11/14/2006 at 6:36 am | Permalink

      import java.io.*;
      class Write {
      public static void main(String[] args) throws Exception {
      File file = new File(”temp.test”);
      FileOutputStream stream = new FileOutputStream(file);
      // write integers here. . .

      }
      }

      Answer:
      DataOutputStream filter = new DataOutputStream(stream);
      for (int i = 0; i

    25. Alok Pandey
      Posted 11/14/2006 at 6:43 am | Permalink

      what is the difference between
      1. Abstract class and Interface
      2. doGet and doPost
      3. Override and Overload
      4. Runnable and Thread
      5. Jsp and Servlet
      6. Hashmap and Hashtable

    26. Rohit
      Posted 11/17/2006 at 11:38 am | Permalink

      Question 2: I think the right answer is “like”
      because
      [0]=”i”
      [1]=”like”
      [3]=”test”

    27. Nick
      Posted 11/28/2006 at 1:01 am | Permalink

      A good question - what is a difference between notify and notifyAll ?

      Give two examples of code where it is a correct thing to use one of them but not another one

    28. Sreenivas.P
      Posted 1/3/2007 at 11:39 pm | Permalink

      ? Interfaces and abstract classes seem superficially to provide almost the same capability. How do you decide which to use ?
      ———————–
      When To Use Interfaces ?
      ———————
      Ans:An interface allows somebody to start from scratch to implement your interface or implement your interface in some other code whose original or primary purpose was quite different from your interface. To them, your interface is only incidental, something that have to add on to the their code to be able to use your package.
      —————————–
      When To Use Abstract classes ?
      ——————————
      An abstract class, in contrast, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. The catch is, code using it must use your class as the base. That may be highly inconvenient if the other programmers wanting to use your package have already developed their own class hierarchy independently. In Java, a class can inherit from only one base class.

      ——————
      When to Use Both ?
      ——————
      You can offer the best of both worlds, an interface and an abstract class. Implementors can ignore your abstract class if they choose. The only drawback of doing that is calling methods via their interface name is slightly slower than calling them via their abstract class name

    29. dip
      Posted 1/19/2007 at 11:49 pm | Permalink

      Q.what is System.out.println?

      System is an member of java.lang class and println is the member of PrintStream class in the package of java.io. out is an object of println.So through out System communicates with println.

    30. Srinivasan
      Posted 1/23/2007 at 1:17 am | Permalink

      1. Which
      of the following are valid definitions of an application’s main( ) method?

      a) public static void main();
      b) public static void main( String args );
      c) public static void main( String args[] );
      d) public static void main( Graphics g );
      e) public static boolean main( String args[] );

      Ans: all the above declarations will compile successfully except the d option. But if you run the program only c option will run successfully. Here only the d and e are not valid one. if you keep a return value for the e option then it will be the valid one.

      So the answers are a,b,c.

    31. Srinivasan
      Posted 1/23/2007 at 1:20 am | Permalink

      3. Which of the following are Java keywords?
      a) array
      b) boolean
      c) Integer
      d) protect
      e) super

      Ans: boolean, Integer and super are the only java keywords

    32. Srinivasan
      Posted 1/23/2007 at 1:24 am | Permalink

      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
      g) always null until a value is
      assigned

      Ans: g is the answer. It will always print a null value until you assign it explicitly.

    33. Srinivasan
      Posted 1/23/2007 at 1:29 am | Permalink

      5. After the declaration:

      int x;

      the range of x is:
      a) -2^31 to 2^31-1

      b) -2^16 to 2^16 -
      1
      c) -2^32 to 2^32
      d) -2^16 to 2^16
      e) cannot be determined; it depends on
      the machine

      Ans: As you know in java the integer value is 4bytes. An this can be a signed one and unsigned one. So its range will be within -2^31 to 2^31-1 (a is correct)

    34. Miriam Smith
      Posted 1/31/2007 at 8:10 pm | Permalink

      1. Which
      of the following are valid definitions of an application’s main( ) method?
      c) public static void main( String args[] );
      e) public static boolean main( String args[] );

      2. If MyProg.java were compiled as an
      application and then run from the command line as:

      java MyProg I like tests

      what would be the value of args[ 1 ] inside
      the main( ) method?

      c) “like”

      3. Which of the following are Java keywords?

      b) boolean
      e) super

      4. After the declaration:
      char[] c = new char[100];
      what is the value of c[50]?

      ‘\u0000′ is the default value
      g) always null until a value is
      assigned

      5. After the declaration:

      int x;

      the range of x is:
      a) -2^31 to 2^31-1

      6. Which identifiers are valid?
      a) _xpoints
      b) r2d2
      c) bBb$
      e) thisisCrazy

      7. Represent the number 6 as a hexadecimal
      literal.
      int x = 0×0006;

      8. Which of the following statements assigns “Hello Java” to the
      String variable s?
      a) String s = “Hello Java”;
      d) String s = new String(”Hello Java”);

      9. An integer, x has a binary value (using 1
      byte) of 10011100. What is the binary value of z after these statements:

      int y = 1 >
      performs 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 );

      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” )

      System.out.println( “Equal A” );

      if( s.equals( “Computer” ) )

      System.out.println( “Equal B” );

      d) “Equal B” is the only
      thing that is printed.

      13. Consider the two statements:

      1. boolean passingScore = false && grade == 70;

      2. boolean passingScore = false & grade == 70;

      The expression

      grade == 70

      is evaluated:
      a) in both 1 and 2

      14. Given the variable declarations below:

      byte myByte;
      int myInt;
      long myLong;
      char myChar;
      float myFloat;
      double myDouble;

      Which one of the following assignments would
      need an explicit cast?

      b) myInt = myLong;
      e) myFloat = myDouble;

      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?
      c) (5, 3) (3, 5) (5, 3)

      16. To declare an array of 31 floating
      point numbers representing snowfall for each day of March in Gnome, Alaska,
      which declarations would be valid?

      a) double snow[] = new double[31];
      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 4 )

      { System.out.println( “Test A” );

      }

      else if( val > 9 )

      { System.out.println( “Test B” );

      }

      else System.out.println( “Test C” );

      Which values of val will
      result in “Test C” being printed:

      a) val 9
      e) val = 0
      f) no values for val will be satisfactory

      28. What exception might a wait()
      method throw?
      InterruptedException

      29. For the code:
      m = 0;
      while( m++
      404
      /error.jsp

      4.
      mypackage.MyException
      /error.jsp

    35. Miriam Smith
      Posted 1/31/2007 at 8:16 pm | Permalink

      18. Consider the code below:
      arr[0] = new int[4];
      arr[1] = new int[3];
      arr[2] = new int[2];
      arr[3] = new int[1];
      for( int n = 0; n 4 )
      { System.out.println( “Test A” );
      }
      else if( val > 9 )
      { System.out.println( “Test B” );
      }

      else System.out.println( “Test C” );

      Which values of val will
      result in “Test C” being printed:

      a) val 9
      e) val = 0
      f) no values for val will be satisfactory

      28. What exception might a wait()
      method throw?
      InterruptedException

      29. For the code:
      m = 0;
      while( m++

    36. Nemala Kalyani
      Posted 2/7/2007 at 1:52 am | Permalink

      Hi all,
      I have seen so many posts with different answers. I have answers for the questions which I think are correct to the best of my knowledge.
      Here we go-

      1. Which of the following are valid definitions of an application’s main( ) method?
      Answer: c) public static void main( String args[] );
      Comments: e is incorrect because Boolean is not a valid return value when the method is called from JVM. Other options are syntactically wrong.

      2. If MyProg.java were compiled as an application and then run from the command line as: java MyProg I like tests
      Answer: c) “like”
      Comments: array index starts from zero in Java

      3. Which of the following are Java keywords?
      Answer: b and e

      4. After the declaration: char[] c = new char[100]; what is the value of c[50]?
      Answer: c) ‘\u0000′
      Comments: character arrays are initialized to ‘\u0000’ by default.

      5. After the declaration: int x; the range of x is:
      Answer: -2 power 31 to (2 power 31)-1

      6. Which identifiers are valid?
      Answer: _xpoints
      r2d2
      bBb$
      thisisCrazy
      Comments: identifiers can contain only alphabets, numbers, _ and $. Also, they cannot start with a digit.

      7. Represent the number 6 as a hexadecimal literal.
      Answer: 0×06

      8. Which of the following statements assigns “Hello Java” to the String variable s?
      Answer: a) String s = “Hello Java”;
      d) String s = new String(”Hello Java”);

      9. An integer, x has a binary value (using 1
      byte) of 10011100. What is the binary value of z after these statements:
      int y = 1 > performs 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 );
      Answer: False
      Comments: the s.concat(s2) is lost since its not assigned to any string. Hence they are not equal.

      12. If you compile and execute an application
      with the following code in its main() method:
      String s = new String( “Computer” );

      if( s == “Computer” )
      System.out.println( “Equal A” );
      if( s.equals( “Computer” ) )
      System.out.println( “Equal B” );
      Answer: d) “Equal B” is the only thing that is printed.
      Comments: I don’t knw how to explain it. :D. The catch here is, 2 strings cant be (= =) when new operator is used because a new string is created ( I mean a new reference) but when (equals) is used, the same string is compared and not the location or reference.

      13. Consider the two statements:
      1. boolean passingScore = false && grade == 70;
      2. boolean passingScore = false & grade == 70;
      The expression
      grade == 70
      Answer: d) in 2 but not 1
      Comments: The && operator is called the short circuit operator. It evaluates the second condition only when the first condition returns true. Hence grade = = 70 is not evaluated.

      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 + ” ) ” );
      }
      }

      Answer: c) (5, 3) (3, 5) (5, 3)
      Comments: this is for the same reason that the concept of interchanging x and y does not hold good once the parameters are out of the switchCoords()

    37. Nemala Kalyani
      Posted 2/7/2007 at 2:08 am | Permalink

      16. To declare an array of 31 floating point numbers representing snowfall for each day of March in Gnome, Alaska, which declarations would be valid?
      Answers: a) double snow[] = new double[31];
      d) double[] snow = new double[31];
      Comments: B and C are incorrect because the size should not be declared in the left side.

      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 4 )
      { System.out.println( “Test A” );
      }
      else if( val > 9 )
      { System.out.println( “Test B” );
      }
      else System.out.println( “Test C” );
      Which values of val will result in “Test C” being printed:
      Answers: a) val

    38. Nemala Kalyani
      Posted 2/7/2007 at 2:11 am | Permalink

      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 4 )
      { System.out.println( “Test A” );
      }
      else if( val > 9 )
      { System.out.println( “Test B” );
      }
      else System.out.println( “Test C” );
      Which values of val will result in “Test C” being printed:
      Answers: a) val

    39. Waqas Ahmad
      Posted 3/8/2007 at 12:05 am | Permalink

      Q # 1:c
      Q # 2:b
      Q # 3:b and e
      Q # 4:g
      Q # 5:c
      Q # 6:b and e

      Q # 8:a and b

      Q # 11:True
      Q # 12:d
      Q # 13:c

      Q # 15:c
      Q # 16:a and d
      Q # 17:d
      Q # 18:c
      Q # 19:e

    40. h.s.
      Posted 3/19/2007 at 1:10 pm | Permalink

      #
      Anirban Basak said,

      What will be the output if you try to compile and run this:

      …..
      int i = 9;
      i = ++i;
      i = i++;
      System.out.println(i);
      …..

      Ans. it will print 10, NOT 11 in JAVA.

      to the moderators: it will print 11 in c/c++ but 10 in JAVA. I dont know the reason. If anybody knows, plz mailme.

      —————————————-
      This is because i = ++i will increment i before assigning it there for it is equivalent of saying i = 10; then it is i = i++ this means that i will be incremented after i is used so it will be like saying i = 10 then increment i which becomes 11 but then the assignment to i makes it 10 again. To try it just change the variable name from i to something else and you will see that the order of execution is the key point.

    41. Joe
      Posted 3/19/2007 at 1:35 pm | Permalink

      To Anirban Basak

      in your case you assin to the same variable

      -Joe

    42. Sharath chandra
      Posted 5/14/2007 at 5:36 am | Permalink

      @mushtak

      Q: Difference between abstract and interface..

      A: In Abstract class, its methods may contain some code part which is not possible in interfaces. sometimes an abstract class needs to be inherited from other class. in that case it cannot use multiple inheritance. but, an interface can hav multiple inheritance. Abstract classes cannot be instantiated.
      An interface is a proper abstract class.

    43. Casttro
      Posted 10/18/2007 at 6:26 am | Permalink

      Few answers from Waqas Ahmad are Wrong, Plz correct it

      Q # 1:c
      Q # 2:b
      Q # 3:b,c and e
      Q # 4:g
      Q # 5:c
      Q # 6:a,b,c and e

      Q # 8:a and d, B will never be right, Can’t assign a string to String array.

      Q # 11:True
      Q # 12:d
      Q # 13:c

      Q # 15:c
      Q # 16:a and d
      Q # 17:d
      Q # 18:c
      Q # 19:e

    44. sameer
      Posted 1/2/2008 at 7:43 am | Permalink

      hi plz give me the response of my ans.& feedback

    45. Vasanthy
      Posted 2/28/2008 at 2:19 pm | Permalink

      Consider the two statements:

      1. boolean passingScore = false && grade == 70; 2. boolean passingScore = false & grade == 70;The expression

      grade == 70

      is evaluated:
      e) invalid because false should be
      FALSE

    46. Posted 6/10/2008 at 12:34 am | Permalink

      question no-64
      Which
      of the following are methods of the Cookie Class?

      Ans- setMaxAge

    47. Posted 6/10/2008 at 12:40 am | Permalink

      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

      ANS-c-
      d-

    48. Sibananda pani
      Posted 12/2/2008 at 7:10 am | Permalink

      the answers for upto 32 are :

      Java/J2EE Programmer Practice Test 1 (page 2)

      1. Which of the following are valid definitions of an application’s main( ) method?

      a) public static void main(); //compile but no main()
      b) public static void main( String args ); //same
      c) public static void main( String args[] ); //works
      d) public static void main( Graphics g );
      //not compiled n for this u hav to import java.awt.graphics
      e) public static boolean main( String args[] );
      // works but must return Boolean value…

      2. If MyProg.java were compiled as an application and then run from the command line as:
      java MyProg I like tests
      what would be the value of args[ 1 ] inside the main( ) method?

      a) MyProg
      b) “I”
      c) “like” (correct)
      d) 3
      e) 4
      f) null until a value is assigned
      3. Which of the following are Java keywords?

      a) array //no
      b) boolean //yes
      c) Integer //false
      d) protect //false
      e) super //true
      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
      g) always null until a value is assigned //true

      DOUBT 5. After the declaration:
      int x;
      the range of x is:

      a) -231 to 231-1
      b) -216 to 216 - 1
      c) -232 to 232
      d) -216 to 216
      e) cannot be determined; it depends on the machine

      6. Which identifiers are valid?

      a) _xpoints
      b) r2d2
      c) bBb$
      d) set-flow //error
      e) thisisCrazy

      7. Represent the number 6 as a hexadecimal literal.

      8. Which of the following statements assigns “Hello Java” to the String variable s?

      a) String s = “Hello Java”; //correct
      b) String s[] = “Hello Java”; //false //cant convert type
      c) new String s = “Hello Java”; //false wrong literal new..
      d) String s = new String(”Hello Java”); //correct

      9. An integer, x has a binary value (using 1 byte) of 10011100. What is the binary value of z after these statements:
      int y = 1 <> performs signed shift while >>> performs an unsigned shift. //correct
      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 ); //it will not store thts why It displays
      Only hello ,if we store in another var then displat all.
      True
      False //correct…

      12. If you compile and execute an application with the following code in its main() method:
      String s = new String( “Computer” );

      if( s == “Computer” )
      System.out.println( “Equal A” );
      if( s.equals( “Computer” ) )
      System.out.println( “Equal B” );
      a) It will not compile because the String class does not support the = = operator.
      b) It will compile and run, but nothing is printed.
      c) “Equal A” is the only thing that is printed.
      d) “Equal B” is the only thing that is printed. //correct
      e) Both “Equal A” and “Equal B” are printed.

      13. Consider the two statements:
      1. boolean passingScore = false && grade == 70;
      2. boolean passingScore = false & grade == 70;
      The expression
      grade == 70
      is evaluated:

      a) in both 1 and 2
      b) in neither 1 nor 2 //correct (&& not defined for
      Boolean and integer)
      c) in 1 but not 2
      d) in 2 but not 1
      e) invalid because false should be FALSE

      14. Given the variable declarations below:
      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; //required
      c) myByte = 3;
      d) myInt = myChar;
      e) myFloat = myDouble; //false
      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) (5, 3) (5, 3) (5, 3)
      b) (5, 3) (3, 5) (3, 5)
      c) (5, 3) (3, 5) (5, 3) //correct

      16. To declare an array of 31 floating point numbers representing snowfall for each day of March in Gnome, Alaska, which declarations would be valid?

      a) double snow[] = new double[31]; //correct
      b) double snow[31] = new array[31];
      c) double snow[31] = new array;
      d) double[] snow = new double[31]; //correct

      String[] s;
      // illegal initialization
      s = { “abc”, “def”, “hij”);

      int[] arr = new int[] {1,2,3}; // legal

      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) Returns the index of the highest element in the array
      b) Returns true/false if there are any elements that repeat in the array
      c) Returns how many even numbers are in the array
      d) Returns the highest element in the array //correct
      e) Returns the number of question marks in the array

      18. Consider the code below:
      arr[0] = new int[4];
      arr[1] = new int[3];
      arr[2] = new int[2];
      arr[3] = new int[1];
      for( int n = 0; n < 4; n++ )
      System.out.println( /* what goes here? */ );
      Which statement below, when inserted as the body of the for loop, would print the number of values in each row?

      a) arr[n].length();
      b) arr.size;
      c) arr.size -1;
      d) arr[n][size];
      e) arr[n].length; //correct

      19. If size = 4, triArray looks like: //doubt

      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 4 )
      { System.out.println( “Test A” );
      }
      else if( val > 9 )
      { System.out.println( “Test B” );
      }
      else System.out.println( “Test C” );

      Which values of val will result in “Test C” being printed:
      a) val 9
      e) val = 0 //true
      f) no values for val will be satisfactory
      28. What exception might a wait() method throw?

      // InterruptedException

      29. For the code:

      m = 0;
      while( m++ < 2 )
      System.out.println( m );

      Which of the following are printed to standard output?

      a) 0
      b) 1 //print
      c) 2 //print
      d) 3
      e) Nothing and an exception is thrown

      30. Consider the code fragment below:

      outer: for( int i = 1; i <3; i++ )
      { inner: for( j = 1; j < 3; j++ )
      { if( j==2 )
      continue outer;
      System.out.println( “i = ” +i “, j = ” + j );
      }
      }

      Which of the following would be printed to standard output?

      a) i = 1, j = 1 //correct
      b) i = 1, j = 2
      c) i = 1, j = 3
      d) i = 2, j = 1 //correct
      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”
      c) “Done with exceptions”
      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();

      if( tree instanceof Pine )
      System.out.println( “Pine” );

      if( tree instanceof Tree )
      System.out.println( “Tree” );

      if( tree instanceof Oak )
      System.out.println( “Oak” );

      else System.out.println( “Oops” );
      }
      }
      Select all choices that will be printed:

      a) Pine //print
      b) Tree //print
      c) Forest
      d) Oops //print
      e) (nothing printed).

      If u wnt more then send mail to :

      sibanada.pani@yahoo.com

    49. nick
      Posted 1/5/2009 at 4:44 pm | Permalink

      What will be the output if you try to compile and run this:

      …..
      int i = 9;
      i = ++i;
      i = i++;
      System.out.println(i);
      …..

      Ans. it will print 10, NOT 11 in JAVA.
      =====================================
      My understending of this “phenomena” is that there is a temporary hidden oject involved when it comes to increment.
      Hence, in second case (i++) this temp object will just hang around doing nothing.

    50. nick
      Posted 1/5/2009 at 4:48 pm | Permalink

      More to the previous answer.
      We deal with Autoboxing here!

    Post a Comment

    Your email is never published nor shared. Required fields are marked *

    *
    *