Java/J2EE Programmer Practice Test 1

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 <> 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, triArray looks 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 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?

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

Advertisements

Clock Applet Program!!

import java.applet.*;
import java.awt.*;
import java.util.*;

public class ClockApplet extends Applet implements Runnable{
 Thread t,t1;
 public void start(){
  t = new Thread(this);
  t.start();
 }
 
 public void run(){
  t1 = Thread.currentThread();
  while(t1 == t){
   repaint();
   try{
    t1.sleep(1000);  
   }catch(InterruptedException e){}
  }
 }

 public void paint(Graphics g){
  Calendar cal = new GregorianCalendar();
  String hour = String.valueOf(cal.get(Calendar.HOUR));
  String minute = String.valueOf(cal.get(Calendar.MINUTE));
  String second = String.valueOf(cal.get(Calendar.SECOND));
  g.drawString(hour + “:” + minute + “:” + second, 20, 30);
 }
}

Simple interest program for JAVA

class SimpleInterest

{
public static void main(String args[])
{
float principle, no_of_years, rate_of_interest, simple_interest;
principle = 10000;
no_of_years= 6;
rate_of_interest = 8.5F;
simple_interest = principle * no_of_years * rate_of_interest ;
simple_interest = simple_interest / 100 ;
System.out.println(“\n Simple Interest on a principle of “+principle+” is “+simple_interest+” at the rate of “+rate_of_interest+” for a period of “+no_of_years);

}
}