Short-answer Questions for JAVA

Wednesday, September 15, 2010

1. 	What will be the output of the following code?
		byte x = 64, y;
		y = (byte) (x << 2);
		System.out.println(y);

2. What will be the output of the following code: byte b; double d = 417.35; b = (byte) d; System.out.println(b);
3. Given the value of a variable, write a statement, without using if construct, which will produce the absolute value of the variable.
4. What is wrong with the following code? switch(x) { case 1: n1 = 10; n2 = 20; case 2: n3 = 30; break; n4 = 40; }
5. What will be the output of the following program code? int m = 100; int n = 300; while (++m < — —n); System.out.println(m);
6. What does the following fragment display String s = “six:” + 3 + 3; System.out.println(s);
7. What is the output of the following code? String s; System.out.println(“s = ” + s);
8. What is the output of the following code? String s = new String( ); System.out.println(“s = ” + s);
9. What is the problem with the following snippet? class Q { public static void main(String args[ ]) { int i = 5, j = 10; if ((i<=j)||(i=10)); System.out.println(“OK”); System.out.println(“NOT OK”); } }
10. What will be the output of the following code snippet? int x = 10; int y = 20; if ((x10) System.out.println(x); else System.out.println(y
11. Show the output the following code: int a, b; a = 5; b = 10; if(a > 5) if(b > 5) { System.out.println(“b is ” + b); } else System.out.println(“a is ” + a);
12. State the output of the following code: int a = 10; int b = 5; if(a > b) { if(b > 5) System.out.println(“b is ” + b); } else System.out.println(“a is” + a);
13. Give the output of the following code: int m = 100; while(true) { if(m < 10) break; m = m — 10; } Systm.out.println(“m is ” + m);
14. Give the output of the following code: int m = 100; while(true) { if(m < 10) continue; m = m — 10; } System.out.println(“m is ” + m);
15. Using a single line of code, complete the following class so that it returns x+y if the value of x is equal to y, otherwise returns 0: public class XY { public return int fun(int x, int y) { .......... (one line code her) } }
16. Given a package named EDU.Student, how would you import a class named Test contained in this package? Write one line statement.
17. Consider the following class defi nition: class Student { abstract double result( ) } This code will not compile since a keyword is missing in the fi rst line. What is the keyword?
18. Consider the following class fi le? import java.awt.*; import java.io.*; package studentBase; class Test { void display( ) { System.out.println(“RESULTS”); } } Will it compile? YES or NO. Give reason, if No:
19. Consider the following code: class Product { public static void main(String args [ ]) { int x = 10, y = 20; System.out.println(mu1 (x, y)); } int mul(int a, int b) { return(a * b); } } Will it compile? YES or NO. Give reason, if No:
20. Given below are two fi les: File Employee.java package purchase; public class Employee { protected double age = 35.00; } File Company.java import purchase.Employee; public class Company { public static void main(String arg[ ]) { Employee e = new Employee( ); System.out.println(“Age = ”+e.age); } } Will the fi le Company.java compile? YES or NO. Give reason, if No.
21. Consider the following code: class A { void method(int x) { System.out.println(“x = ” + x); } } class B extends A { void method(int y) { System.out.println(“y = ” + y); } void method(String s) { System.out.println(“s = ” + s); } public static void main(String args[ ]) { A a1 = new A( ); A a2 = new B( ); a1.method(10); a2.method(20); } } What will be the output, when executed?
22. There are three classes that implement and DataInput and DataOutput interfaces. Two of them are DataInputStream and DataOutputStream. Which is the third one?
23. What output will the following program produce? class Bits { public static void main(String args[ ]) { short s1 = 3; // 0000 0011 short s2 = 13; // 0000 1101 s1 = (short) (s1 ^ s2); System.out.println(“Result is”+s1); } }
24. State the output of the following program: class Condition { public static void main(String args[ ]) { int x = 10; int y = 15; System.out.println((x>y)?3.14:3)); } }
25. Which of the classes in java.io package defines a method to delete a fi le?
26. Given a valid File object reference, we can create a new fi le using two classes defi ned in java.io package. One is FileOutputStream class. Which is the other one?
27. If raf is an instance of RandomAccessFile, how can we move the fi le pointer to the end of the fi le? Write the statement
28. What will be the output of the following program when it is executed with the command line java Command Java is wonderful class Command { public staitic void main(String args[ ]) { for(int i = 1; i < args.length; i++) { System.out.print(args[i]); if( i != args.length ) { System.out.print(“ ”); }System.out.println(“ ”); } }
29. What will be the output of the following code snippet when combined with suitable declarations and run? StringBuffer city = new StringBuffer(“Madras”); StringBuffer string = new StringBuffer( ); string.append(new String(city)); string.insert(0, “Central ”); String.out.println(string);
30. Consider the following program code: class Thread1 extends Thread { public void run( ) { System.out.println(“Begin”); suspend( ); resume( ); System.out.println(“End”); } } class ThreadTest { public static void main(String args[ ]) { Thread1 T1 = new Thread1( ); T1.start( ); } } On execution, what will be the output?
31. Consider the following application: class Max { public static void main(String args[ ]) { int max = 10; max(max, 20, 30); System.out.println(max); } static void max(int max, int x1, int x2) { if(x1 > x2) max = x1; else max = x2; } } What value is printed out, when executed?
32. State the output of the following program: class Recur { public static void main(String args[ ]) { int Result = result(10); System.out.println(“Result = ”+Result); } static int result(int m) { if (m <= 2) return m; else return m + result(m—2); } }
33. Consider the class defi nition: class Default { public static void main(String args[ ]) { int m; System.out.println(“m is ” + m); } } Will this code compile? YES or NO. Give reason, if No.
34. What is the output of the following program? class Static { static int m = 0; static int n = 0; public static void main(String args[ ]) { int m = 10; int x = 20; { int n = 30; System.out.println(“m+n=”+m+n); } x = m + n; System.out.println(“x = ” + x); } }
35 Consider the following class defi nitions: class Square { private square( ) { }int area(int side) { return(side * side); } } class Constructor { public static void main(String args[ ]) { Square S1 = new Square( ); int area = S1.area(10); System.out.println(area); } } Will the code above compile and run successfully. YES or NO. Give reason, if No.
36. Write a statement to draw a rounded rectangle with the following features: width = 200 height = 100 corner horizontal diameter = 20 corner vertical diameter = 40 Select a suitable upper-left corner of the rectangle
37. Which line of the following HTML fi le contains an error? 1. < applet 2. WIDTH = 400 HEIGHT = 200 3. CODE = HelloJava.Class > 4. < param 5. NAME = “string” 6. VALUE = “Hello” >
38. Give the output of the following program: class MainString { public static void main(String args[ ]) { StringBuffer s = new StringBuffer(“String”); if(s.length()>5) && (s.append(“Buffer”).equals(“X”); // empty statement System.out.println(s); } }
39. What is the range of the value that can be assigned to a variable of type long?
40. Consider the following program : class Number { int x; void store(Number num) { num.x++; } } class MainNumber { public static void main(String args[ ]) { Number n = new Number( ); n.x = 10; n.store(n); System.out.println(n.x); } } What is the output?
41. Given the code: class Continue { public static void main(String args[ ]) { int m = 0; loop1: for(int i=0; i<10; i++) loop2: for(int j=0;j<10;j++) loop3: for(int k=0;k<10;k++) { System.out.println(++m); if( (k%10) == 0) continue loop2; } } } What is the last value printed?
42. Can an abstract method by declared fi nal? YES or NO. If NO, give reason.
43. Can an abstract method be declared static? YES or NO. If NO, give reason.
44. Consider the following try ... catch block: class TryCatch { public static void main(String args[ ]) { try { double x = 0.0; throw(new Exception(“Thrown”)); return; } catch(Exception e) { System.out.println(“Exception caught”); return; } fi nally { System.out.println(“fi nally”); } } } What will be the output?
45. Write a statement that would construct a 20 point bold Helvetica font.

0 comments: