Thursday, 24 September 2015

java quiz-2


java quiz-2



1
class Test
{
public static void main(String args[])
{
byte g=3;
switch(g)
{
case 3:
System.out.printf("java");
case 129:
System.out.printf("for");
}
System.out.printf("ocpjp");
}
}
java
javaforocpjp
CompileError
RunTimeException


2
class Test
{
boolean sum()
{
for(int i=0;i<3;i++)
{
System.out.printf("java");
return true;//line y
}
return false;//line x
}
public static void main(String args[])
{
Test t1=new Test();
boolean fp=t1.sum();
 }
}
java
javajavajava
compilation fails due to line x
compilation fails due to line y


3
class Test
{
public static void main(String args[])
{
int x=5;
switch(x)
{
case 1:
System.out.println("Nature");
break;
case 5:
System.out.println("Beauty");
continue;
}
System.out.println("AXXL");
}
}
Compilation fails
execute infinte times
Beauty AXXL
Beauty


4
class Test
{
public static void main(String args[])
{
int arr[]=new int[5];
try
{
for(int i=0;i<=5;i++)
System.out.printf(arr[i]);
}
catch(Exception e)
{
e.printStackTrace();
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
}
000000
012345
Compilation fails
An exception is thrown at run-time


5
class Exc15
{
static int n=5;
static int sum()
{
return --n;
}
static void go()
{
System.out.println("baba");
}
public static void main(String args[])
{
for(int i=0;i<=10;i++)
{
int ans=sum();
assert ans>0 :go();
System.out.println(ans);
}
}
}
4321 for more information of the exception it will print baba
Runtime exception
4321
Compile Error


6
class Exc15
{
static int n=5;
static int sum()
{
return --n;
}
static int go()
{
return 25;
}
public static void main(String args[])
{
for(int i=0;i<=10;i++)
{
int ans=sum();
assert ans>0 :go();
System.out.print(ans);
}
}
}
runtime exception
comile error
it will print 4321
it will print 4321 and for more information of the exception it will print 25


7
class Exc15
{
static int n=5;
static int sum()
{
return --n;
}
static void go()
{
System.out.println("baba");
}
public static void main(String args[])
{
for(int i=0;i<=10;i++)
{
int ans=sum();
assert ans>0 :new Exc15();
System.out.print(ans);
}
}
}
it will print 4321 and for more information of the exception it will print hashcode of the object
it will print 4321 and terminate
it will print 4321 only
comile time error


8
public class Test {
public static void main(String [] args) {
int I = 1;
do while ( I < 1 )
System.out.print("I is " + I);
while ( I > 1 ) ;
}
}
compilation error at line 4
1
No output is produced
compilation error at line 6


9
1. import java.io.*;
2. public class MyProgram {
3. public static void main(String args[]){
4. FileOutputStream out = null;
5. try {
6. out = new FileOutputStream("test.txt");
7. out.write(122);
8. }
9. catch(IOException io) {
10. System.out.println("IO Error.")
11. }
12. finally {
13. out.close();
14. }
15. }
16. }
This program fails to compile due to an error at line 4.
This program fails to compile due to an error at line 13.
This program will compile successfully.
This program fails to compile due to an error at line 9.


10
class Exc15
{
static int n=5;
static int sum()
{
return --n;
}
static void go()
{
System.out.println("baba");
}
public static void main(String args[])
{
for(int i=0;i<=10;i++)
{
int ans=sum();
assert ans>0 :Exc15 e
System.out.print(ans);
}
}
}
compile time error
1234
it will print 1234 and then print the refernce of class Exc15
runtime Exception

No comments:

Post a Comment