A Java Course Outline
Using the Java By Dissection book
by Ira Pohl and Charlie McDowell

 



Exam 2 Sample

SYNTAX QUESTION:

Identify and correct any syntax error and no other. Points will be taken off for unnecessary corrections. Treat each line as separate.

   if x == m  return 1;

   while (x < 5 ) { y = x; --y = --x;}
   
   if (x < -1) { y = ( x - 7)} else z;

   int i = 5.0, j-k = 6;
  

What does this program print?
 class Test{ 
   Static int M = 2; 

  static int foo( int a ) { return (a * a); } 
  static int goo1( int x ) { return foo(foo(x + 1)); } 
  static int goo2( int x, int y ) { return  foo(x + goo1(y + 1)); } 

 	public static void main(String[] args) 
 	{ 
    	 int i = 2, j = 1; 

   	 	 System.out.println(" foo = " + foo(i)); 
   		 System.out.println(" foo = " + foo(i + M)); 
   	 	 System.out.println(" goo1 = "  + goo1(i + M)); 
    	 System.out.println(" goo2 =  " + goo2(i, j)); 
    	 System.out.println(" goo2 =  " + goo2(j, i)); 
 	} 
 } 
 

Answer true or false.

a) long is a keyword. b) A method is defined at class scope. c) The expression x == 0.5 is an int expression. d) The expression 3.0/1.5 is an int expression. e) _abc is an identifier.


What will be printed by the following program?
 class Problem4{ 
 	public static void main(String[] args)  
 	{ 
	     int i; 
	
	     for( i = 0; i < 10; i++ ) 
	         if (i++ == 7) 
	             break; 
	         else if (i < 3) 
	             /*  do nothing */; 
	         else 
	             System.out.println("i is " +   ++i);  //tricky 
	     System.out.println("Final value: " + i); 
	   
 	} 

 } 

For each of following tokens, specify whether it is a legal identifier, a keyword, an operator, or none of these.
token identifier keyword operator other
FirstNumber        
a + b        
third__        
4th        
And        
%        
__ 2        
System.out.println        
double        
real        

Assume the following declarations: int i = 1, j = 3, k = 4;

Fill in the value of each expression or illegal (assume they are independent, in other words, each expression should be thought of as going immediately after the declarations and not affecting a following expression).  

I < 0 && j > 2 ______ 
 j % i ______
 i / j ______ 
2 * i + 1 / j______
 j <= k ______ 
!!k ______ 
(i + j) * k-- ______ 
k > 2 && i > 6 ______
 k = j++ ______ 
k = ++j ______ 

What does the following program print ?

 class P6{ 
 	static int sum; 
	public static void main(String[] args) 
 	{ 
 		int i = 5, j; 
       	sum = 0; 

 		for ( j = 0; i <= 0; i--)  { 
 			sum += i * i; 
 			System.out.println ("sum =  " + sum); 
 		} 
 	} 
 ) 

What gets printed?
 class P8{ 
 public static void main(String[] args) 
    { 
     int   i = 3, j = 6, k = 2; 

     if (i != 3) if (j == 6) 
     	System.out.println(i = i + j % k); 
     else 
     	System.out.println(i = i - j % k ); 
     	System.out.println(i + 2); 
     	System.out.println(i = i + j % k); 
   } 
}

Given the following classes answer:
 public class Counter { 
   int v = 0;  //   0 <= v < 100 
   public Counter() { 
       v = 0; 
   } 
   public Counter(int inVal) { 
       v = inVal % 100; 
   } 
   int get(){ return v;} //accessor 
   void set(int setVal){ v = setVal % 100; } 
   void click(){ v = ++v % 100;} 
   void printCounter() 
   { 
      System.out.println(" Counter value is " + v); 
   } 
 } 
 
 public class CounterTest{ 
     static public void main(String[] args)  
     { 
        Counter c1 = new Counter(); 
        Counter c2 = new Counter(50); 
        c1.printCounter(); 
        c2.printCounter(); 

     } 

 } 

What gets printed? _____ 

What would happen to c3.value if we had Counter c3 = new Counter(101);

 Explain why int value was not declared static.

After a declaration without a new Counter c4; what reference value does c4 have?


Feel free to report any site problems to the Webmaster, Debra Dolsberry.