jobs4timesLogo jobs4timesLogo

Language Fundamentals

Question : 1

public class Foo {
	
 static void alpha(){
	//code here
 }
 void beta(){
	//code here
 }

}//class
Which two statements are true ?
  1. Foo.beta() is a valid invocation of beta()
  2. Foo.alpha() is a valid invocation of alpha()
  3. method beta() can directly call method alpha()
  4. method alpha() can directly call method beta()

Answer : B, C

Question : 2

public class Yipee {	
public static void main(String[] yahoo){

 for(int x=1; x < yahoo.length; x++){
  System.out.println(yahoo[x]+"");
 }
 
}//main
}//class


and the command line  invocation :
 java Yipee a b c
What is the Result ?
  1. a b
  2. b c
  3. a b c
  4. Compilation fails
  5. An exception is thrown at runtime.

Answer : B

Question : 3

public class Yipee {	
public static void main(String[] args){

 for(int x=1; x < args.length; x++){
  System.out.println(args[x]+"");
 }
 
}//main
}//class


and two seperate command line invocations :
 java Yipee
 java Yipee 1 2 3 4 
What is the result ?
  1. No output is produced
    1 2 3
  2. No output is produced
    2 3 4
  3. No output is produced
    1 2 3 4
  4. An exception is thrown at runtime
    1 2 3
  5. An exception is thrown at runtime
    2 3 4
  6. An exception is thrown at runtime
    1 2 3 4

Answer : B

Question : 4

Given class Reception :

package utils;

public class Repetition {	
 public static String twice(String s){
   return s+s;
 }
 
}//class


and given another  class Demo :

// insert code here line 1

public class Demo {	
public static void main(String[] args){
  System.out.println(twice("pizza")); 
}//main
}//class
Which code should be inserted at line 1 of Demo.java to compile and run Demo to print "pizzapizza" ?
  1. import utils.*;
  2. static import utils.*;
  3. import utils.Repetition.*;
  4. static import utils.Repetition.*;
  5. import utils.Repetition.twice();
  6. import static utils.Repetition.twice;
  7. static import utils.Repetition.twice;

Answer : F

Question : 5

A JavaBeans component has the following field :
private boolean enabled;
Which two pairs of method declaration follow the JavaBeans standard for accessing this field ? (Choose two)
  1. public void setEnabled(boolean enabled)
    public boolean getEnabled()
  2. public void setEnabled(boolean enabled)
    public void isEnabled()
  3. public void setEnabled(boolean enabled)
    public boolean isEnabled()
  4. public boolean setEnabled(boolean enabled)
    public boolean isEnabled()

Answer : A, C

Question : 6

Given classes defined in two different files :
package util;

public class BitUtils {	
public static void process(byte[] bytes){
	//..........
}
	
}


package app;

public class SomeApp {	
public static void main(String[] args){
 byte[] bytes=new byte[256];
   
  // insert code here
}
}

What is required at insert code here in class SomeApp to use the process method of BitUtils ?
  1. process(bytes);
  2. BitUtils.process(bytes);
  3. util.BitUtils.process(bytes);
  4. SomeApp cannot use methods in BitUtils.
  5. import util.BitUtils.*; process(bytes);

Answer : C

Question : 7

Given :
enum Example{ONE,TWO,THREE}
Which statement is true ?
  1. The expressions (ONE==ONE) and ONE.euels(ONE) are both guaranteed to be true.
  2. The expression (ONE < TWO) is guaranteed to be true and ONE.compareTo(TWO) is guaranteed to be less than one.
  3. The Example values cannot be used in a raw java.util.HashMap; instead, the programmer must use a java.util.EnumMap.
  4. The Example values can be used in a java.util.Sortedset, but the set will NOT be sorted because enumerated types do NOT implement java.lang.Comparable.

Answer : A

Question : 8

Given :
public abstract class Shape {
	
 private int x;
 private int y;
 
public abstract void draw();

public void setAnchor(int x,int y){
  this.x=x;
  this.y=y;
}
	
}
Which two classes use the Shape class correctly ? (Choose two)
  1. public class Circle implements Shape {
     private int radius;
    }
    
  2. public abstract class Circle extends Shape {
     private int radius;
    }
    
  3. public class Circle extends Shape {
     private int radius;
     public void draw();
    }
    
  4. public abstract class Circle implements Shape {
     private int radius;
     public void draw();
    }
    
  5. public class Circle extends Shape {
     private int radius;
     
     public void draw(){ 
        //.........
     }
    
    }
    
  6. public abstract class Circle implements Shape {
     private int radius;
     
     public void draw(){ 
        //.........
     }
    
    }
    

Answer : B, E

Question : 9

Given :
class Nav{
 public enum Direction{NORTH,SOUTH,EAST,WEST}
}
public class Sprite{
 //insert code here	
}
Which code, inserted at insert code here , allows the Sprite class to compile ?
  1. Direction d=NORTH;
  2. Nav.Direction d=NORTH;
  3. Direction d=Direction.NORTH;
  4. Nav.Direction d=Nav.Direction.NORTH;

Answer : D

Question : 10

interface Foo{
  int bar();
}

public class Beta{
	
 class A implements Foo{
  public int bar(){ return 1; }
 }
 
 public int fubar(Foo foo){
	 return foo.bar();
 }
 
 public void testFoo(){
  class A implements Foo{
	  public int bar(){return 2; }	  
  }
  System.out.println(fubar(new A()));
 }//testFoo() 
 
 
 public static void main(String[] args){
	 new Beta().testFoo();
 }	
	
}
Which three ststements are true ? (Choose three)
  1. Compilation fails
  2. The code compiles and the output is 2.
  3. If below lines were removed, compilation would fail.
    class A implements Foo{
      public int bar(){ return 1; }
    }
    
  4. If below lines were removed, compilation would fail.
    class A implements Foo{
      public int bar(){return 2; }	  
    }
    
  5. If below lines were removed, the code would compile and the output would be 2.
    class A implements Foo{
      public int bar(){ return 1; }
    }
    
  6. If below lines were removed, the code would compile and the output would be 1.
    class A implements Foo{
      public int bar(){return 2; }	  
    }
    

Answer : B, E, F

Question : 11

interface Reloadable{
 public void reload();
}

class Edit{	
 public void edit(){
	 //......
 }
}

interface Displayable 
         XXXXX Xxxxxxxx {
  xxxxxxxxx
}
Code Fragments :
  1. extends
  2. public void display();
  3. Reloadable
  4. implements
  5. public void display(){ /* display*/ }
  6. Edit

solution :

interface Reloadable{
 public void reload();
}

class Edit{	
 public void edit(){
	 //......
 }
}

interface Displayable extends Reloadable{
	public void display();
}

Question : 12

Given :
1  int[] x={1,2,3,4,5};
2  int y[]=x;
3  System.out.println(y[2]);
Which statement is true ?
  1. Line 1 will print the value 2
  2. Line 2 will print the value 3
  3. Compilation will fail because of an error in Line 1
  4. Compilation will fail because of an error in Line 2

Answer : B

Question : 13

Given :
1  String #name="Jone Doe";
2  int $age=24;
3  Double _height=123.5;
4  double ~temp=37.5;
Which two statements are true ? (Choose two)
  1. Line 1 will not compile
  2. Line 2 will not compile
  3. Line 3 will not compile
  4. Line 4 will not compile

Answer : A, D

Question : 14

A programmer needs to create a logging method that can accept an arbitrary number of arguments.

For example, it may be called in these ways :
logIt("log message1");
logIt("log message2","log message3");
logIt("log message4","log message5","log message6");
Which declaration satisfies this requirement ?
  1. public void logIt(String * msgs)
  2. public void logIt(String[] msgs)
  3. public void logIt(String... msgs)
  4. public void logIt(String msgs1,String msgs2,String msgs3)

Answer : C

Question : 15


Which two code fragments correctly create and initialize a static array of int elements ? (Choose two)
  1. static final int[] a={100,200};
  2. static final int[] a;
    static{ a=new int[2]; a[0]=100; a[1]=200; }
  3. static final int[] a=new int[2]{100,200};
  4. static final int[] a;
    static void init(){ a=new int[3]; a[0]=100; a[1]=200; }

Answer : A, B

Question : 16

public static void main(String[] args){
	
 String str="null";
if(str==null){
 System.out.println("null");
}
else(str.length()==0){
 System.out.println("zero");
}
else{
 System.out.println("some");
}
 
}
What is the result ?
  1. null
  2. zero
  3. some
  4. Compilation fails
  5. An exception is thrown at runtime

Answer : D

Question : 17

Given

Test t=new Test();
t.method(5);


What is the output of the Test class ?
public class Test{
	
int x=12;
 public void method(int x){
  x+=x;
  System.out.println(x);
 }

}
  1. 5
  2. 10
  3. 12
  4. 17
  5. 24

Answer : B

Question : 18

Given :
1 public interface Status{
2  /*insert code here*/ int MY_VALUE=10;
3 }
Which three are valid on on Line 2 ? (Choose three)
  1. final
  2. static
  3. native
  4. public
  5. private
  6. abstract
  7. protected

Answer : A, B, D

Question : 19

A programmer is designing a class to encapsulate the information about an inventory item.
A JavaBeans component is needed to do this.
The InventoryItem class has private instance variables to store the item information :
private int itemId;
private String name;
private String description;
Which method signature follows the JavaBeans naming standards for modifying the itemId instance variable ?
  1. itemId(int itemId)
  2. update(int itemId)
  3. setItemId(int itemId)
  4. mutateItemId(int itemId)
  5. updateItemId(int itemId)

Answer : C

Question : 20

Given file  GrizzlyBear.java :

1 package animals.mammals;
2
3 public class GrizzlyBear extends Bear{
4 void hunt(){
5	Salmon s=findSalmon();
6	s.consume();
7 }
8 }

and another file, Salmon.java

1 package animals.fish;
2
3 public class Salmon extends Fish{
4  void consume() { /* do stuff */ }
5 }
Assume both classes are defined in the correct directories for theft packages, and that the Mammal class correctly defines the findSalmon() method. Which two changes allow this code to compile correctly ? (Choose two)
  1. add public to the start of line 4 in Salmon.java
  2. add public to the start of line 4 in GrizzlyBear.java
  3. add import animals.mammals.*; at line 2 in Salmon.java
  4. add import animals.fish.*; at line 2 in GrizzlyBear.java
  5. add import animals.fish.Salmon.*; at line 2 in GrizzlyBear.java
  6. add import animals.mammals.GrizzlyBear.*; at line 2 is Salmon.java

Answer : A, D

Question : 21

Given :
public class Barn{
public static void main(String[] args){
 new Barn().go("hi",1);
 new Barn().go("hi","world",2);
}

public void go(String... y, int i) {
 System.out.println(y[y.length-1]+" ");	
}

}
What is the result ?
  1. hi hi
  2. hi world
  3. world world
  4. Compilation fails
  5. An exception is thrown at runtime.

Answer : D

Question : 22

Given :
1 class Mud{
2 //insert code here
3   System.out.println("hi");
4  }
5 }

And the following five fragments :

public static void main(String...a){
public static void main(String.*a){  //not valid
public static void main(String... a){
public static void main(String[]... a){
public static void main(String...[] a){ //not valid 
How many of the code fragments, inserted independently at Line 2, compile ?
  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5

Answer : D

Question : 23

Given :
class Alligator{	
public static void main(String[] args){
 int []x[]={{1,2},{3,4,5},{6,7,8,9}};
 int [][]y=x;
  System.out.println(y[2][1]);
 }
}
What is the result ?
  1. 2
  2. 3
  3. 4
  4. 6
  5. 7
  6. Compilation fails

Answer : E

Question : 24

public class Venus{	
public static void main(String[] args){
 int []x={1,2,3};
 int y[]={4,5,6};
new Venus().go(x,y); 
}

void go(int[]... z) {
 for(int[] a:z)
  System.out.println(a[0]);
 }

}
What is the result ?
  1. 1
  2. 1 2
  3. 1 4
  4. 1 2 3
  5. Compilation fails
  6. An exception is thrown at runtime.

Answer : C

Question : 25


Which two code fragments correctly create and initialize a static array of int elements ? (Choose two)
  1. static final int[] a={100,200};
  2. static final int[] a;
    static { a=new int[2]; a[0]=100; a[1]=200; }
  3. static final int[] a=new int[2]{100,200};
  4. static final int[] a; static void init(){ a=new int[3]; a[0]=100; a[1]=200; }

Answer : A, B

Question : 26

Which are valid declarations ? (Choose all that apply)
  1. int $x;
  2. int 123;
  3. int _123;
  4. int #dim;
  5. int %percent;
  6. int *divide;
  7. int central_sales_region_Summer_2005_gross_sales;

Answer : A, C, G

  • A,C,G are legal identifiers.
  • B is incorrect because an identifier can't start with a digit.
  • D,E,F are incorrect because identifiers must start with $, _, or a letter.

Question : 27

Which method names follow the JavaBeans standard ? (Choose all the apply)
  1. addSize
  2. getCust
  3. deleteReq
  4. isColorado
  5. putDimentions

Answer : B, D

  • B and D use the valid prefixes 'get' and 'is'
  • A,C and E are incorrect because 'add', 'delete' and 'put' are not standard JavaBeans name prefixes.

Question : 28

1 public class Voop{	
2 public static void main(String[] args){
3	doStuff(1);
4	doStuff(1,2);
5 }
6 //insert code here	
7 }
Which, inserted independently at Line 6, will compile ? (Choose all that apply)
  1. static void doStuff(int... doArgs){ }
  2. static void doStuff(int[] doArgs){ }
  3. static void doStuff(int doArgs){ }
  4. static void doStuff(int... doArgs,int y){ }
  5. static void doStuff(int x,int... doArgs){ }

Answer : A, E

  • A and E use valid var-args syntax.
  • B and C are invalid var-arg syntax, and D is invalid because the var-arg must be the last of a method's arguments.

Question : 29

Which are legal declarations ? (Choose all that apply)
  1. short x[];
  2. short[] y;
  3. short[5] x2;
  4. short z2[5];
  5. short[] z[][];
  6. short[] y2=[5];

Answer : A, B, E

  • A, B and E are correct array declarations; E is a three dimensional array.
  • C, D and F are incorrect, you can't include the size of your array in a declaration unless you also instantiate the array object.F uses invalid instantiation syntax.
BACK