Make your own free website on Tripod.com
1- 
public class DemoAssert1 {
	private static int a =15;
	private static int b =12;
		
	static boolean evaluateFirst() {
		if(a>b) return true; else return false;
	}
	
	static boolean evaluateSecond() {
		if(a<b) return true; else return false;
	}

	static Object shouldDo() {
		return "Yeah";	
	}
	
	static boolean shouldNotDo() {
		return false;
	}
	public static void main(String [] args) {
		assert evaluateFirst(): shouldDo();	//Line 1
		assert evaluateSecond(): shouldNotDo();	 //Line 2
	}
	
}
Select the correct answer
A: Compilation fails at Line 1, the second expression of assert can't be an object
B: Compilation fails at Line 2, the second expression of assert can't be boolean 
C: Both lines show a compilation error because the second expression of an assert statement can't be a method call that returns something.
D: The program compiles fine if shouldDo() and shouldNotDo() is set to return void
E: Program compiles fine and shows runtime exception due to failure at Line 2.
F: None of the above

2- 

public class DemoAssert2 {

	public static void main(String... args) {
		final int a;
		a=10;
		assert a>19?false:false:false;	
	}

}
What will be the output of the given code if assertion is enabled

A: Compilation fails because final variable a is not initialized when declared
B: Compilation fails due to illegal assert expression 
C: Compilation succeeds but at runtime AssertionError exception is thrown.
D: None of the above


3- 

public class DemoAssert3 {
	
	static String message1() {
		return "I don't know why 1<2;";		
	}

	static String message2() {
		return "I know but can't express";	
	}
	public static void main(String... args) {
		assert true: message()+message2;	//Line 1
		assert true: message();			//Line 2
	}  
}

What is the result if ea is enabled?
(Choose all that aplly)
A: Fails to compile due to error on Line 1 
B: Compiles fine and prints "I don't know why 1<2;I know but can't express" with AssertionError exception  
C: Both lines will always throw AssertionError exception because both assert with true.
D: Compiles fine and runs without printing anything
E: None of the above

4-

public class DemoAssert4 {
	static void message() {
		System.out.println("Assertion error arose");
	}
	public static void main(String[] args) {
		assert true:message();	//Line 1
		assert false:message(); //Line 2
	}
}
(Choose all that aplly)
A: Compiler error at Line 1
B: Compiler error at Line 2
C: Runtime exception with message "Assertion error arose"
D: Compiles and run but no output.
E: Runtime exception 
F: None of the above

5- 

public class DemoAssert5 { 
	final public static void main(String[] args) {
		assert false: "Assertions are good for testing purpose!";
	}
}
(Choose all that aplly)
A: Won't compile main() can't be final
B: Compiles and run and no exception till assert is false
C: Prints 'Assertions are good for testing purpose' only when we write:
assert true : "Assertions are good for testing purpose!" with AssertionError at run time
D: Compiles fine with runtime exception with message "Assertions are good for testing purpose".

6- 

public class DemoAssert6 {
	public static void main(String[] args) {
		assert true:false; //Line 1		
		assert false:true; //Line 2	
	}

}
A: Compilation fails at line 1 
B: Compilation fails at line 2
C: Runtime exception due to line 1
D: Runtime exception due to line 2
E: Non of the above

7- 

public class DemoAssert7 {
	
	private void doStuff1(int x) {
		assert(x>0); 
	}
	
	public void doStuff2(int x) {
		assert (x>0);	
	}  
	
}
(Choose all that apply)
A: Compilation can't start, there is no main() method defined.
B: assert is not used appropriately in doStuff1(int x) method.
C: assert is not used appropriately in doStuff2(int x) method.
D: Compiles fine but Runtime exception.
E: assert is being used illegally in both the methods.

8-

public class DemoAssert8 {

	public void doStuff1(int x) {
		int y=0;
		switch(x) {
			case 1: y=1;break;
			case 2: y=4;break;
			case 3: y=9;break;
			default: assert false;
		}
	}
	
	private void doStuff2(int x) {
		int y=0;
		switch(x) {
			case 4: y=16;break;
			case 5: y=25;break;
			case 6: y=36;break;
			default: assert false;
		}
	}

}
(Choose all that apply)
A: assert has been used inappropriately in doStuff1() method.
B: assert has been used inappropriately in doStuff2() method.
C: assert has been used inappropriately in both the methods. 
D: assert has been used illegally in doStuff1() method.
E: assert has been used illegaly in doStuff2() method.
F: assert has been used illegally in both the methods.
G: Non of the above.

9-
public class DemoAssert9 {

	public static void main(String... args) {
		assert(args[0]); //Line 1
	}
}
class invocation with:
java DemoAssert9 false true false
(Choose two that apply)
A- assert has been used inappropriately at Line 1
B- assert has been used illegally at Line 1
C- assert has been used legally.
D- assert has been used appropriately.
E- None of the above

10- 

public class DemoAssert10 {
	int y;
	int x;	
	private void doStuff() {
		assert(modifyThings());
	}

	public boolean modifyThings() {
		y=x++;
		return true;		
	}

}
(Choose all that apply)
A: assert has been used inappropriately in method doStuff()
B: assert has been used appropriately in method doStuff()
C: It is illegal use of assert in method doStuff()
D: It is legal use of assert in method doStuff()
E: None of the above

-------------------------------------------------------------------------------------------------
I HOPE YOU WOULD HAVE ATTEMPTED ALL THE QUESTIONS BEFORE YOU COME HERE TO SATISFY YOU AS WELL AS YOUR ANSWERS :)

Answers:
1- E
2- C
3- D
4- A,B
5- D
6- D
7- C,D
8- G
9- A,B (assert requires boolean, passed String, hence compiler error; if it were boolean
it is not appropriate to use assertion to validate command line argument because it is 
not sure that assertion is enabled)
10- A,D

Dated: 08/March/2007
Don't forget to send your comment on my this little effort at: cmbhatt@imap.cc

-------------------------------------------------------------------------------------------------