JavaCore Test 7 Which of the following is not a return type? Button public void boolean What would happen when the following is compiled and executed. public class Compare { public static void main(String args[]) { int x = 10, y; if(x = 10) y = 2; System.out.println("y is " + y); } } The program does not compile complaining about y not being initialized. The program compiles and prints y is 2 when executed. The program compiles and prints y is 0 when executed. The program compiles and prints y is 1 when executed. Given two non-negative integers a and b and a String str, what is the number of characters in the expression str.substring(a,b). a - b b - a a + b b - a - 1 Which operator is used to perform bitwise exclusive or. ^ | ! & What all gets printed when the following program is compiled and run. public class test { public static void main(String args[]) { int i=0, j=2; do { i=++i; j--; } while(j>0); System.out.println(i); } } The program does not compile because of statement "i=++i;" 0 1 2 If result = 2 + 3 * 5, what is the value and type of "result" variable? 17, int 25, byte 17, byte 25, int What is the data type for the number 9.6352? Double Float float double What would happen when the following is compiled and executed. class example { int x; int y; String name; public static void main(String args[]) { example pnt = new example(); System.out.println("pnt is " + pnt.name + " " + pnt.x + " " + pnt.y); } } The program prints pnt is null 0 0. The program does not compile because x, y and name are not initialized. The program prints pnt is 0 0. The program throws a runtime exception as x, y, and name are used before initialization. The initial value of an instance variable of type String that is not explicitly initialized in the program is --. The instance variable must be explicitly assigned. NULL "" null The initial value of a local variable of type String that is not explicitly initialized and which is defined in a member function of a class. null "" NULL The local variable must be explicitly assigned. What is the result of compiling and running the following program. class test { public static void main(String args[]) { char ch; String test2 = "abcd"; String test = new String("abcd"); if(test.equals(test2)) { if(test == test2) ch = test.charAt(0); else ch = test.charAt(1); } else { if(test == test2) ch = test.charAt(2); else ch = test.charAt(3); } System.out.println(ch); } } 'c' 'd' 'b' 'a' What gets displayed on the screen when the following program is compiled and run. public class test { public static void main(String args[]) { boolean x = true; int a; if(x) a = x ? 1: 2; else a = x ? 3: 4; System.out.println(a); } } 4 3 2 1 What is the result of compiling and running the following program. class test { public static void main(String args[]) { int i,j=0; for(i=10;i<0;i--) { j++; } switch(j) { case (0) : j=j+1; case(1): j=j+2; break; case (2) : j=j+3; break; case (10) : j=j+10; break; default : break; } System.out.println(j); } } 1 2 3 0 What gets displayed on the screen when the following program is compiled and run. public class test { public static void main(String args[]) { boolean x = false; int a; if(x) a = x ? 1: 2; else a = x ? 3: 4; System.out.println(a); } } 1 3 4 2 What all gets printed when the following gets compiled and run. public class test { public static void main(String args[]) { int i=1, j=1; try { i++; j--; if(i/j > 1) i++; } catch(ArithmeticException e) { System.out.println(0); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(1); } catch(Exception e) { System.out.println(2); } finally { System.out.println(3); } System.out.println(4); } } a. 0 b. 1 c. 2 d. 3 e. 4 a, c, d a, d, e a, b, c b, c, d Assume that the value 3929.92 is of type "float". How to assign this value after declaring the variable "interest" of type float? interest = (Float)3929.92 interest = 3929.92 interest = 3929.92 (float) interest = 3929.92f Which of the following are legal Java programs. a. // The comments come before the package package pkg; import java.awt.*; class C{} b. package pkg; import java.awt.*; class C{} c. package pkg1; package pkg2; import java.awt.*; class C{} d. package pkg; import java.awt.*; e. import java.awt.*; class C{} f. import java.awt.*; package pkg; class C {} c, d, e, f b, c, d, e a, b, c, d a, b, d, e What is the number displayed when the following program is compiled and run. class test { public static void main(String args[]) { test test1 = new test(); System.out.println(test1.xyz(100)); } public int xyz(int num) { if(num == 1) return 1; else return(xyz(num-1) + num); } } 5055 4040 4045 5050 What gets displayed on the screen when the following program is compiled and run. public class test { public static void main(String args[]) { int x, y; x = 5 >> 2; y = x >>> 2; System.out.println(y); } } 0 2 5 80 What all gets printed when the following gets compiled and run. public class test { public static void main(String args[]) { int i=1, j=1; try { i++; j--; if(i == j) i++; } catch(ArithmeticException e) { System.out.println(0); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(1); } catch(Exception e) { System.out.println(2); } finally { System.out.println(3); } System.out.println(4); } } a. 0 b. 1 c. 2 d. 3 e. 4 d, e c, d a, b b, c Schreibe einen Kommentar Antworten abbrechenDu musst angemeldet sein, um einen Kommentar abzugeben.