13066332-Java-Question

Share Embed Donate


Short Description

Download 13066332-Java-Question...

Description

Question 1) Which of the following lines will compile without warning or error. 1) float f=1.3; 2) char c="a"; 3) byte b=257; 4) boolean b=null; 5) int i=10; 5 Question 2) What will happen if you try to compile and run the following code public class MyClass { public static void main(String arguments[]) { amethod(arguments); } public void amethod(String[] arguments) { System.out.println(arguments); System.out.println(arguments[1]); } } 1) error Can't make static reference to void amethod. 2) error method main not correct 3) error array must include parameter 4) amethod must be declared with String 1 Question 3) Which of the following will compile without error 1) import java.awt.*; package Mypackage; class Myclass {} 2) package MyPackage; import java.awt.*; class MyClass{} 3) /*This is a comment */ package MyPackage; import java.awt.*; class MyClass{} 2 Question 4)

A byte can be of what size 1) -128 to 127 2) (-2 power 8 )-1 to 2 power 8 3) -255 to 256 4)depends on the particular implementation of the Java Virtual machine 1 Question 5) What will be printed out if this code is run with the following command line? java myprog good morning public class myprog{ public static void main(String argv[]) { System.out.println(argv[2]) } } 1) myprog 2) good 3) morning 4) Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2" 4 Question 6) Which of the following are keywords or reserved words in Java? 1) if 2) then 3) goto 4) while 5) case Question 7) Which of the following are legal identifiers 1) 2variable 2) variable2 3) _whatavariable 4) _3_ 5) $anothervar 6) #myvar 2,3,4,5 Question 8) What will happen when you compile and run the following code?

public class MyClass{ static int i; public static void main(String argv[]){ System.out.println(i); } } 1) Error Variable i may not have been initialized 2) null 3) 1 4) 0 0 Question 9) What will happen if you try to compile and run the following code? public class Q { public static void main(String argv[]){ int anar[]=new int[]{1,2,3}; System.out.println(anar[1]); } } 1) 1 2) Error anar is referenced before it is initialized 3) 2 4) Error: size of array must be defined 2 Question 10) What will happen if you try to compile and run the following code? public class Q { public static void main(String argv[]){ int anar[]=new int[5]; System.out.println(anar[0]); } } 1) Error: anar is referenced before it is initialized 2) null 3) 0 4) 5 3

Question 11) What will be the result of attempting to compile and run the following code? abstract class MineBase { abstract void amethod(); static int i; } public class Mine extends MineBase { public static void main(String argv[]){ int[] ar=new int[5]; for(i=0;i < ar.length;i++) System.out.println(ar[i]); } } 1) a sequence of 5 0's will be printed 2) Error: ar is used before it is initialized 3) Error Mine must be declared abstract 4) IndexOutOfBoundes Error 3 Question 12) What will be printed out if you attempt to compile and run the following code ? int i=1; switch (i) { case 0: System.out.println("zero"); break; case 1: System.out.println("one"); case 2: System.out.println("two"); default: System.out.println("default"); } 1) one 2) one, default 3) one, two, default 4) default 3 Question 13) What will be printed out if you attempt to compile and run the following code? int i=9; switch (i) {

default: System.out.println("default"); case 0: System.out.println("zero"); break; case 1: System.out.println("one"); case 2: System.out.println("two"); } 1) default 2) default, zero 3) error default clause not defined 4) no output displayed Question 14) Which of the following lines of code will compile without error 1) int i=0; if(i) { System.out.println("Hello"); } 2) boolean b=true; boolean b2=true; if(b==b2) { System.out.println("So true"); } 3) int i=1; int j=2; if(i==1|| j==2) System.out.println("OK"); 4) int i=1; int j=2; if(i==1 &| j==2) System.out.println("OK"); 1,2,3 Question 15) What will be output if you try to compile and run the following code, but there is no file called Hello.txt in the current directory?. import java.io.*; public class Mine {

public static void main(String argv[]){ Mine m=new Mine(); System.out.println(m.amethod()); } public int amethod() { try { FileInputStream dis=new FileInputStream("Hello.txt"); }catch (FileNotFoundException fne) { System.out.println("No such file found"); return -1; }catch(IOException ioe) { } finally{ System.out.println("Doing finally"); } return 0; } } 1) No such file found 2 No such file found ,-1 3) No such file found, Doing finally, -1 4) 0 Question 16) Which of the following statements are true? 1) Methods cannot be overriden to be more private 2) Static methods cannot be overloaded 3) Private methods cannot be overloaded 4) An overloaded method cannot throw exceptions not checked in the base class Question 17) What will happen if you attempt to compile and run the following code? 1) Compile and run without error 2) Compile time Exception 3) Runtime Exception class Base {} class Sub extends Base {} class Sub2 extends Base {} public class CEx{ public static void main(String argv[]){ Base b=new Base(); Sub s=(Sub) b; } }

Runtime Exception

Question 18) If the following HTML code is used to display the applet in the code MgAp what will be displayed at the console? 1) Error: no such parameter 2) 0 3) null 4) 30 import java.applet.*; import java.awt.*; public class MgAp extends Applet{ public void init(){ System.out.println(getParameter("age")); } } null Question 19) You are browsing the Java HTML documentation for information on the java.awt.TextField component. You want to create Listener code to respond to focus events. The only Listener method listed is addActionListener. How do you go about finding out about Listener methods? 1) Define your own Listener interface according to the event to be tracked 2) Use the search facility in the HTML documentation for the listener needed 3) Move up the hierarchy in the HTML documentation to locate methods in base classes 4) Subclass awt.event with the appropriate Listener method Question 20) What will be displayed when you attempt to compile and run the following code //Code start import java.awt.*; public class Butt extends Frame{ public static void main(String argv[]){

Butt MyBut=new Butt(); } Butt(){ Button HelloBut=new Button("Hello"); Button ByeBut=new Button("Bye"); add(HelloBut); add(ByeBut); setSize(300,300); setVisible(true); } } //Code end 1) Two buttons side by side occupying all of the frame, Hello on the left and Bye on the right 2) One button occupying the entire frame saying Hello 3) One button occupying the entire frame saying Bye 4) Two buttons at the top of the frame one saying Hello the other saying Bye 4 Question 21) What will be output by the following code? public class MyFor{ public static void main(String argv[]){ int i; int j; outer: for (i=1;i 32 -32 0 d> 0 0 0 Q 10) What is the output (Assuming written inside main)

String s1 = new String("amit"); String s2 = s1.replace('m','i'); s1.concat("Poddar"); System.out.println(s1); System.out.println((s1+s2).charAt(5)); a) Compile error b) amitPoddar o c) amitPoddar i d) amit i Q 11) What is the output (Assuming written inside main) String s1 = new String("amit"); System.out.println(s1.replace('m','r')); System.out.println(s1); String s3="arit"; String s4="arit"; String s2 = s1.replace('m','r'); System.out.println(s2==s3); System.out.println(s3==s4); a) arit amit false true b) arit arit false true c) amit amit false true d) arit amit true true Q12) Which one does not extend java.lang.Number 1)Integer 2)Boolean 3)Character 4)Long 5)Short

Q13) Which one does not have a valueOf(String) method 1)Integer 2)Boolean 3)Character 4)Long 5)Short Q.14) What is the output of following (Assuming written inside main) String s1 = "Amit"; String s2 = "Amit"; String s3 = new String("abcd"); String s4 = new String("abcd"); System.out.println(s1.equals(s2)); System.out.println((s1==s2)); System.out.println(s3.equals(s4)); System.out.println((s3==s4)); a) true true true false b) true true true true c) true false true false Q15. Which checkbox will be selected in the following code ( Assume with main and added to a Frame) Frame myFrame = new Frame("Test"); CheckboxGroup cbg = new CheckboxGroup(); Checkbox cb1 = new Checkbox("First",true,cbg); Checkbox cb2 = new Checkbox("Scond",true,cbg); Checkbox cb3 = new Checkbox("THird",false,cbg); cbg.setSelectedCheckbox(cb3); myFrame.add(cb1); myFrame.add(cb2); myFrame.add(cb3); a) cb1 b) cb2,cb1 c) cb1,cb2,cb3 d) cb3

Q16) Which checkbox will be selected in the following code ( Assume with main and added to a Frame) Frame myFrame = new Frame("Test"); CheckboxGroup cbg = new CheckboxGroup(); Checkbox cb1 = new Checkbox("First",true,cbg); Checkbox cb2 = new Checkbox("Scond",true,cbg); Checkbox cb3 = new Checkbox("THird",true,cbg); myFrame.add(cb1); myFrame.add(cb2); myFrame.add(cb3); a) cb1 b) cb2,cb1 c) cb1,cb2,cb3 d) cb3 Q17) What will be the output of line 5 1 Choice c1 = new Choice(); 2 c1.add("First"); 3 c1.addItem("Second"); 4 c1.add("Third"); 5 System.out.println(c1.getItemCount()); a) 1 b) 2 c) 3 d) None of the above Q18) What will be the order of four items added Choice c1 = new Choice(); c1.add("First"); c1.addItem("Second"); c1.add("Third"); c1.insert("Lastadded",2); System.out.println(c1.getItemCount()); a) First,Second,Third,Fourth b) First,Second,Lastadded,Third c) Lastadded,First,Second,Third Q19) Answer based on following code 1 Choice c1 = new Choice(); 2 c1.add("First"); 3 c1.addItem("Second"); 4 c1.add("Third"); 5 c1.insert("Lastadded",1000); 6 System.out.println(c1.getItemCount()); a) Compile time error b) Run time error at line 5

c) No error and line 6 will print 1000 d) No error and line 6 will print 4 Q20) Which one of the following does not extends java.awt.Component a) CheckBox b) Canvas c) CheckbocGroup d) Label Q21) What is default layout manager for panels and applets? a) Flowlayout b) Gridlayout c) BorderLayout Q22) For awt components which of the following statements are true? a) If a component is not explicitly assigned a font, it usese the same font that it container uses. b) If a component is not explicitly assigned a foreground color , it usese the same foreground color that it container uses. c) If a component is not explicitly assigned a backround color , it usese the same background color that it container uses. d) If a component is not explicitly assigned a layout manager , it usese the same layout manager that it container uses. Q23)java.awt.Component class method getLocation() returns Point (containg x and y cordinate).What does this x and y specify a) Specify the postion of components lower-left component in the coordinate space of the component's parent. b) Specify the postion of components upper-left component in the coordinate space of the component's parent. c) Specify the postion of components upper-left component in the coordinate space of the screen. Q24. Q. What will be the output of follwing { double d1 = -0.5d; System.out.println("Ceil for d1 " + Math.ceil(d1)); System.out.println("Floor for d1 " +Math.floor(d1)); } Answers: a) Ceil for d1 0 Floor for d1 -1; b) Ceil for d1 0 Floor for d1 -1.0; c) Ceil for d1 0.0 Floor for d1 -1.0;

d) Ceil for d1 -0.0 Floor for d1 -1.0; Q25. What is the output of following { float f4 = -5.5f; float f5 = 5.5f; float f6 = -5.49f; float f7 = 5.49f; System.out.println("Round f4 is " + Math.round(f4)); System.out.println("Round f5 is " + Math.round(f5)); System.out.println("Round f6 is " + Math.round(f6)); System.out.println("Round f7 is " + Math.round(f7)); } a)Round f4 is -6 Round f5 is 6 Round f6 is -5 Round f7 is 5 b)Round f4 is -5 Round f5 is 6 Round f6 is -5 Round f7 is 5 Q26. Given Integer.MIN_VALUE = -2147483648 Integer.MAX_VALUE = 2147483647 What is the output of following { float f4 = Integer.MIN_VALUE; float f5 = Integer.MAX_VALUE; float f7 = -2147483655f; System.out.println("Round f4 is " + Math.round(f4)); System.out.println("Round f5 is " + Math.round(f5)); System.out.println("Round f7 is " + Math.round(f7)); } a)Round f4 is -2147483648 Round f5 is 2147483647 Round f7 is -2147483648 b)Round f4 is -2147483648 Round f5 is 2147483647 Round f7 is -2147483655 Q27) 1 Boolean b1 = new Boolean("TRUE"); 2 Boolean b2 = new Boolean("true"); 3 Boolean b3 = new Boolean("JUNK"); 4 System.out.println("" + b1 + b2 + b3);

a) Comiler error b) RunTime error c)truetruefalse d)truetruetrue Q 28) In the above question if line 4 is changed to System.out.println(b1+b2+b3); The output is a) Compile time error b) Run time error c) truetruefalse d) truetruetrue Q 29. What is the output { Float f1 = new Float("4.4e99f"); Float f2 = new Float("-4.4e99f"); Double d1 = new Double("4.4e99"); System.out.println(f1); System.out.println(f2); System.out.println(d1); } a) Runtime error b) Infinity -Infinity 4.4E99 c) Infinity -Infinity Infinity d) 4.4E99 -4.4E99 4.4E99 Q30 Q. Which of the following wrapper classes can not take a "String" in constructor 1) Boolean 2) Integer 3) Long 4) Character 5) Byte 6) Short Q31. What is the output of following Double d2 = new Double("-5.5"); Double d3 = new Double("-5.5"); System.out.println(d2==d3); System.out.println(d2.equals(d3));

a) true true b) false false c) true false d) false true Q32) Which one of the following always honors the components's preferred size. a) FlowLayout b) GridLayout c) BorderLayout Q33) Look at the following code import java.awt.*; public class visual extends java.applet.Applet{ static Button b = new Button("TEST"); public void init(){ add(b); } public static void main(String args[]){ Frame f = new Frame("Visual"); f.setSize(300,300); f.add(b); f.setVisible(true); } } What will happen if above code is run as a standalone application a) Displays an empty frame b) Displays a frame with a button covering the entire frame c) Displays a frame with a button large enough to accomodate its label. Q34 If the code in Q33 is compiled and run via appletviewer what will happen a) Displays an empty applet b) Displays a applet with a button covering the entire frame c) Displays a applet with a button large enough to accomodate its label. Q35. What is the output public static void main(String args[]){ Frame f = new Frame("Visual"); f.setSize(300,300); f.setVisible(true); Point p = f.getLocation(); System.out.println("x is " + p.x); System.out.println("y is " + p.y);

} a) x is 300 y is 300 b) x is 0 y is 0 c) x is 0 y is 300 Q36) Which one of the following always ignores the components's preferred size. a) FlowLayout b) GridLayout c) BorderLayout Q37) Consider a directory structure like this (NT or 95) C:\JAVA\12345.msg --FILE \dir1\IO.class -- IO.class is under dir1 Consider the following code import java.io.*; public class IO { public static void main(String args[]) { File f = new File("..\\12345.msg"); try{ System.out.println(f.getCanonicalPath()); System.out.println(f.getAbsolutePath()); }catch(IOException e){ System.out.println(e); } } } What will be the output of running "java IO" from C:\java\dir1 a) C:\java\12345.msg C:\java\dir1\..\12345.msg b) C:\java\dir1\12345.msg C:\java\dir1\..\12345.msg c) C:\java\dir1\..\12345.msg C:\java\dir1\..\12345.msg Q 38) Suppose we copy IO.class from C:\java\dir1 to c:\java What will be the output of running "java IO" from C:\java. a) C:\java\12345.msg C:\java\..\12345.msg b) C:\12345.msg C:\java\..\12345.msg c) C:\java\..\12345.msg C:\java\\..\12345.msg

Q39) Which one of the following methods of java.io.File throws IOException and why a) getCanonicalPath and getAbsolutePath both require filesystem queries. b) Only getCannonicalPath as it require filesystem queries. c) Only getAbsolutePath as it require filesystem queries. Q40) What will be the output if Consider a directory structure like this (NT or 95) C:\JAVA\12345.msg --FILE \dir1\IO.class -- IO.class is under dir1 import java.io.*; public class IO { public static void main(String args[]) { File f = new File("12345.msg"); String arr[] = f.list(); System.out.println(arr.length); } } a) Compiler error as 12345.msg is a file not a directory b) java.lang.NullPointerException at run time c) No error , but nothing will be printed on screen Q41) What will be the output Consider a directory structure like this (NT or 95) C:\JAVA\12345.msg --FILE import java.io.*; public class IO { public static void main(String args[]) { File f1 = new File("\\12345.msg"); System.out.println(f1.getPath()); System.out.println(f1.getParent()); System.out.println(f1.isAbsolute()); System.out.println(f1.getName()); System.out.println(f1.exists()); System.out.println(f1.isFile()); } } a) \12345.msg \ true 12345.msg true true b) \12345.msg \

true \12345.msg false false c) 12345.msg \ true 12345.msg false false d) \12345.msg \ true 12345.msg false false Q42) If in question no 41 the line File f1 = new File("\\12345.msg"); is replaced with File f1 = new File("12345.msg"); What will be the output a) 12345.msg \ true 12345.msg true true b) 12345.msg null true 12345.msg true true c) 12345.msg null false 12345.msg true true d) \12345.msg \ true 12345.msg false false

ANSWERS :1) a) 2) b) 3) c) 4) b) 5) C) 6) a)As you can not override a method with weaker access privileges 7) c)As you can access a protected variable in the same package. 8) a) Because protected variables and methods can not be accssed in another package directly. They can only be accessed if the class is subclassed and instance of subclass is used. 9) a) 10) d)As String is imutable.so s1 is always "amit". and s2 is "aiit". 11) a) s3==s4 is true because java points both s3 and s4 to same memory location in string pool 12) 2) and 3) 13) 3) 14) a) 15) d) As in a CheckboxGroup only one can be selected 16) d) As in a CheckboxGroup only one can be selected 17) c) 18) b) 19) d) 20) c) 21) a) 22) a),b),c) 23) b) 24) d) as 0.0 is treated differently from -0.0 25) b) 26) a) //Reason If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE. If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE. // From JDK api documentation 27) c) 28) a) As there is no method to support Boolean + Boolean Boolean b1 = new Boolean("TRUE"); Think ----->System.out.println(b1); // Is this valid or not? 29) b) 30) 4) 31) d)

32) a) 33) b) Reason- Frame uses Border Layout which places the button to CENTRE (By default) and ignores Button's preferred size. 34) c) Reason- Applet uses FlowLayout which honors Button's preferred size. 35) b) Because postion is always relative to parent container and in this. case Frame f is the topemost container 36) b) 37) a) as getCanonicalPath Returns the canonical form of this File object's pathname. The precise definition of canonical form is system-dependent, but it usually specifies an absolute pathname in which all relative references and references to the current user directory have been completely resolved. WHERE AS getAbsolutePath Returns the absolute pathname of the file represented by this object. If this object represents an absolute pathname, then return the pathname. Otherwise, return a pathname that is a concatenation of the current user directory, the separator character, and the pathname of this file object. 38) b) 39) b) 40) b) 41) d) 42) c)

Question 1 What will happen if you compile/run this code? 1: public class Q1 extends Thread 2: { 3: public void run() 4: { 5: System.out.println("Before start method"); 6: this.stop(); 7: System.out.println("After stop method"); 8: } 9: 10: public static void main(String[] args) 11: { 12: Q1 a = new Q1(); 13: a.start(); 14: } 15: } A) Compilation error at line 7. B) Runtime exception at line 7. C) Prints "Before start method" and "After stop method". D) Prints "Before start method" only. Question 2 What will happen if you compile/run the following code? 1: class Test 2: { 3: static void show() 4: { 5: System.out.println("Show method in Test class"); 6: } 7:} 8: 9: public class Q2 extends Test 10: { 11: static void show() 12: { 13: System.out.println("Show method in Q2 class"); 14: } 15: public static void main(String[] args) 16: { 17: Test t = new Test(); 18: t.show();

19: 20: 21: 22: 23: 24: 25: 26: 27: } 28: }

Q2 q = new Q2(); q.show(); t = q; t.show(); q = t; q.show();

A) prints "Show method in Test class" "Show method in Q2 class" "Show method in Q2 class" "Show method in Q2 class" B) prints "Show method in Test class" "Show method in Q2 class" "Show method in Test class" "Show method in Test class" C) prints "Show method in Test class" "Show method in Q2 class" "Show method in Test class" "Show method in Q2 class" D) Compilation error.

Question 3 The following code will give 1: class Test 2: { 3: void show() 4: { 5: System.out.println("non-static method in Test"); 6: } 7: } 8: public class Q3 extends Test 9: { 10: static void show() 11: { 12: System.out.println("Overridden non-static method in Q3"); 13: }

14: 15: 16: 17: 18: 19:

public static void main(String[] args) { Q3 a = new Q3(); } }

A) Compilation error at line 3. B) Compilation error at line 10. C) No compilation error, but runtime exception at line 3. D) No compilation error, but runtime exception at line 10. Question No :4 The following code will give 1: class Test 2: { 3: static void show() 4: { 5: System.out.println("Static method in Test"); 6: } 7: } 8: public class Q4 extends Test 9: { 10: void show() 11: { 12: System.out.println("Overridden static method in Q4"); 13: } 14: public static void main(String[] args) 15: { 16: } 17: } A) Compilation error at line 3. B) Compilation error at line 10. C) No compilation error, but runtime exception at line 3. D) No compilation error, but runtime exception at line 10.

Question No :5 The following code will print 1: 2:

int i = 1; i = 31; i >>= 1; int j = 1; j = 31; System.out.println("i = " +i ); System.out.println("j = " +j);

A) i = 1 j=1 B) i = -1 j=1 C) i = 1 j = -1 D) i = -1 j = -1

Question No :6 The following code will print 1: Double a = new Double(Double.NaN); 2: Double b = new Double(Double.NaN); 3: 4: if( Double.NaN == Double.NaN ) 5: System.out.println("True"); 6: else 7: System.out.println("False"); 8: 9: if( a.equals(b) ) 10: System.out.println("True"); 11: else 12: System.out.println("False"); A) True True B) True False

C) False True D) False False

Question No :7 1: if( new Boolean("true") == new Boolean("true")) 2: System.out.println("True"); 3: else 4: System.out.println("False"); A) Compilation error. B) No compilation error, but runtime exception. C) Prints "True". D) Prints "False".

Question No :8 1: public class Q8 2: { 3: int i = 20; 4: static 5: { 6: int i = 10; 7: 8: } 9: public static void main(String[] args) 10: { 11: Q8 a = new Q8(); 12: System.out.println(a.i); 13: } 14: } A) Compilation error, variable "i" declared twice. B) Compilation error, static initializers for initialization purpose only. C) Prints 10. D) Prints 20.

Question No :9 The following code will give 1: 2: 3: 4: 5: 6:

Byte b1 = new Byte("127"); if(b1.toString() == b1.toString()) System.out.println("True"); else System.out.println("False");

A) Compilation error, toString() is not avialable for Byte. B) Prints "True". C) Prints "False".

Question No :10 What will happen if you compile/run this code? 1: public class Q10 2: { 3: public static void main(String[] args) 4: { 5: int i = 10; 6: int j = 10; 7: boolean b = false; 8: 9: if( b = i == j) 10: System.out.println("True"); 11: else 12: System.out.println("False"); 13: } 14: } A) Compilation error at line 9 . B) Runtime error exception at line 9. C) Prints "True". D) Prints "Fasle". Question 11 What will happen if you compile/run the following code? 1: 2: 3:

public class Q11 { static String str1 = "main method with String[] args";

4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:

static String str2 = "main method with int[] args"; public static void main(String[] args) { System.out.println(str1); } public static void main(int[] args) { System.out.println(str2); } }

A) Duplicate method main(), compilation error at line 6. B) Duplicate method main(), compilation error at line 11. C) Prints "main method with main String[] args". D) Prints "main method with main int[] args".

Question 12 What is the output of the following code? 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:

class Test { Test(int i) { System.out.println("Test(" +i +")"); } } public class Q12 { static Test t1 = new Test(1); Test

t2 = new Test(2);

static Test t3 = new Test(3); public static void main(String[] args) { Q12 Q = new Q12(); } }

A) Test(1)

Test(2) Test(3) B) Test(3) Test(2) Test(1) C) Test(2) Test(1) Test(3) D) Test(1) Test(3) Test(2)

Question 13 What is the output of the following code? 1: 2: 3: 4: 5:

int i = 16; int j = 17; System.out.println("i >> 1 = " + (i >> 1)); System.out.println("j >> 1 = " + (j >> 1));

A) Prints "i >> 1 = 8" "j >> 1 = 8" B) Prints "i >> 1 = 7" "j >> 1 = 7" C) Prints "i >> 1 = 8" "j >> 1 = 9" D) Prints "i >> 1 = 7" "j >> 1 = 8"

Question 14 What is the output of the following code? 1: 2: 3:

int i = 45678; int j = ~i;

4:

System.out.println(j);

A) Compilation error at line 2. ~ operator applicable to boolean values only. B) Prints 45677. C) Prints -45677. D) Prints -45679.

Question 15 What will happen when you invoke the following method? 1: 2: 3: 4: 5: 6: 7: 8:

void infiniteLoop() { byte b = 1; while ( ++b > 0 ) ; System.out.println("Welcome to Java"); }

A) The loop never ends(infiniteLoop). B) Prints "Welcome to Java". C) Compilation error at line 5. ++ operator should not be used for byte type variables. D) Prints nothing.

Question 16 In the following applet, how many Buttons will be displayed? 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:

import java.applet.*; import java.awt.*; public class Q16 extends Applet { Button okButton = new Button("Ok"); public void init() { add(okButton); add(okButton); add(okButton); add(okButton);

15: 16: 17: 18: 19: 20: 21: 22:

add(new Button("Cancel")); add(new Button("Cancel")); add(new Button("Cancel")); add(new Button("Cancel")); setSize(300,300); } }

A) 1 Button with label "Ok" and 1 Button with label "Cancel" . B) 1 Button with label "Ok" and 4 Buttons with label "Cancel" . C) 4 Buttons with label "Ok" and 1 Button with label "Cancel" . D) 4 Buttons with label "Ok" and 4 Buttons with label "Cancel" .

Question 17 In the following, which is correct Container-Default layout combination? A) Applet - FlowLayout B) Applet - BorderLayout C) Applet - CardLayout D) Frame - Flowlayout E) Frame - BorderLayout F) Frame - CardLayout G) Panel - FlowLayout H) Panel - BorderLayout.

Question 18 What is the output of the following code? 1: 2: 3: 4: 5:

String str = "Welcome"; str.concat(" to Java!"); System.out.println(str);

A) Strings are immutable, compilation error at line 3. B) Strings are immutable, runtime exception at line 3. C) Prints "Welcome". D) Prints "Welcome to Java!".

Question 19 What is the output of the following code? 1: class MyClass 2: { 3: static int maxElements; 4: 5: MyClass(int maxElements) 6: { 7: this.maxElements = maxElements; 8: } 9: 10: } 11: 12: public class Q19 13: { 14: public static void main(String[] args) 15: { 16: 17: MyClass a = new MyClass(100); 18: MyClass b = new MyClass(100); 19: 20: if(a.equals(b)) 21: System.out.println("Objects have the same values"); 22: else 23: System.out.println("Objects have different values"); 24: } 25: } A) Compilation error at line 20. equals() method was not defined. B) Compiles fine, runtime exception at line 20. C) Prints "Objects have the same values". D) Prints "Objects have different values";

Question 20 1: import java.applet.*; 2: import java.awt.*; 3: 4: public class Q20 extends Applet 5: { 6: Button okButton = new Button("Ok"); 7: 8: public void init() 9: {

10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:

setLayout(new BorderLayout()); add("South", okButton); add("North", okButton); add("East", okButton); add("West", okButton); add("Center", okButon); setSize(300,300); } }

The above Applet will display A) Five Buttons with label "Ok" at Top, Bottom, Right, Left and Center of the Applet. B) Only one Button with label "Ok" at the Top of the Applet. C) Only one Button with label "Ok" at the Bottom of the applet. D) Only one Button with label "Ok" at the Center of the Applet. Question 21 What will happen if you compile/run the following code? 1: public class Q21 2: { 3: int maxElements; 4: 5: void Q21() 6: { 7: maxElements = 100; 8: System.out.println(maxElements); 9: } 10: 11: Q21(int i) 12: { 13: maxElements = i; 14: System.out.println(maxElements); 15: } 16: 17: public static void main(String[] args) 18: { 19: Q21 a = new Q21(); 20: Q21 b = new Q21(999); 21: } 22: }

A) Prints 100 and 999. B) Prints 999 and 100. C) Compilation error at line 3, variable maxElements was not initialized. D) Compillation error at line 19. Question 22 What will happen if run the following code? 1: 2: 3: 4: 6: 7:

Boolean[] b1 = new Boolean[10]; boolean[] b2 = new boolean[10]; System.out.println("The value of b1[1] = " +b1[1]); System.out.println("The value of b2[1] = " +b2[1]);

A) Prints "The value of b1[1] = false" "The value of b2[1] = false". B) Prints "The value of b1[1] = null" "The value of b2[1] = null". C) Prints "The value of b1[1] = null" "The value of b2[1] = false". D) Prints "The value of b1[1] = false" "The value of b2[1] = null".

Question 23 Which of the following are valid array declarations/definitions? 1: int iArray1[10]; 2: int iArray2[]; 3: int iArray3[] = new int[10]; 4: int iArray4[10] = new int[10]; 5: int []iArray5 = new int[10]; 6: int iArray6[] = new int[]; 7: int iArray7[] = null; A) 1. B) 2. C) 3. D) 4. E) 5. F) 6.

G) 7. Question 24 What is the output for the following lines of code? 1: System.out.println(" " +2 + 3); 2: System.out.println(2 + 3); 3: System.out.println(2 + 3 +""); 4: System.out.println(2 + "" +3); A) Compilation error at line 3 B) Prints 23, 5, 5 and 23. C) Prints 5, 5, 5 and 23. D) Prints 23, 5, 23 and 23.

Question 25 The following declaration(as a member variable) is legal. static final transient int maxElements = 100; A) True. B) False.

Question 26 What will happen if you compile/run the following lines of code? 1: int[] iArray = new int[10]; 2: 3: iArray.length = 15; 4: 5: System.out.println(iArray.length); A) Prints 10. B) Prints 15. C) Compilation error, you can't change the length of an array. D) Runtime exception at line 3. Question 27 What will happen if you compile/run the folowing lines of code? 1: Vector a = new Vector(); 2: 3: a.addElement(10); 4: 5: System.out.println(a.elementAt(0));

A) Prints 10. B) Prints 11. C) Compilation error at line 3. D) Prints some garbage. Question 28 What will happen if you invoke the following method? 1: public void check() 2: { 3: System.out.println(Math.min(-0.0,+0.0)); 4: System.out.println(Math.max(-0.0,+0.0)); 5: System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0)); 6: } A) prints -0.0, +0.0 and false. B) prints -0.0, +0.0 and true. C) prints 0.0, 0.0 and false. D) prints 0.0, 0.0 and true. Question 29 What will happen if you compile/run this code? 1: int i = 012; 2: int j = 034; 3: int k = 056; 4: int l = 078; 5: 6: System.out.println(i); 7: System.out.println(j); 8: System.out.println(k); A) Prints 12,34 and 56. B) Prints 24,68 and 112. C) Prints 10, 28 and 46. D) Compilation error. Question 30 When executed the following line of code will print System.out.println(-1 * Double.NEGATIVE_INFINITY); A) -Infinity B) Infinity C) NaN D) -NaN

ANSWERS Question No: 1 D. After the execution of stop() method, thread won't execute any more statements. Question No: 2 D. Explicit casting is required at line 25. Question No: 3 B. You cann't override an non-static method with static method. Question No: 4 B. You cann't override a static method with non-static method. Question No: 5 D. Question No: 6 C. Question No: 7 D. Question No: 8 D. Here the variable '"i" defined in static initializer is local to that block only. The statements in the static initializers will be executed (only once) when the class is first created. Question No: 9 C. Question No: 10 C. Conditional operators have high precedence than assignment operators. Question No 11 C. Here the main method was overloaded, so it won't give compilation error. Question No 12 D. No matter where they declared, static variables will be intitialized before non-static variables.

Question No 13 A. 16 >> 1 is 8 and 17 >> 1 also 8. Question No 14 D. Java allows you to use ~ operator for integer type variables. The simple way to calculate is ~i = (- i) - 1. Question No 15 B. Here the variable 'b' will go upto 127. After that overflow will occur, so 'b' will be set to -ve value, the loop ends and prints "Welcome to Java" Question No 16 B. Question No 17 A, E and G. For Applets and Panels FlowLayout is the default one, BorderLayout is default for Window and Frames. Question No 18 C. Strings are immutable. So str.concat("to Java!") will not append anything to str. Infact it will create another string "Welcome to Java!" and leaves it. Question No 19 D. equals() method was available in base class Object. So it won't give any compilation error. Here MyClass is a user-defined class, so the user has to implement equals() method according to his requirments. Question No 20 D. Question No 21 D. Constructors should not return any value. Java won't allow to indicate with void. In this case void Q21() is an ordinary method which has the same name of the Class. Question No 22 C. By default objects will be initialized to null and primitives to their corresponding default vaulues. The same rule applies to array of objects and primitves. Question No 23

B,C,E and G. You can't specify the array dimension in type specification(left hand side), so A and D are invalid. In line 6 the array dimension is missing(right hand side) so F is invalid. You can intialize an array with null. so G is valid. Question No 24 B. Question No 25 A. Question No 26 C. Once array is created then it is not possible to change the length of the array. Question No 27 C. You can't add primitives to Vector. Here 10 is int type primitive. Question No 28 B. The order of floating/double values is -Infinity --> Negative Numbers/Fractions --> -0.0 --> +0.0 --> Positive Numbers/Fractions --> Infinity. Question No 29 D. Here integers are assinged by octal values. Octal numbers will contain digits from 0 to 7. 8 is illegal digit for an octal value, so you get compilation error. Question No 30 B. Compile and see the result.

QUESTIONS :1.Which statement about the garbage collection mechanism are true? A.Garbage collection require additional programe code in cases where multiple threads are running. B.The programmer can indicate that a referance through a local variable is no longer of interest. C.The programmer has a mechanism that explicity and immediately frees the memory used by Java objects. D.The garbage collection mechanism can free the memory used by Java Object at explection time. E.The garbage collection system never reclaims memory from objects while are still accessible to running user thread. 2.Give the following method: 1) public void method(){ 2) String a,b; 3) a=new String("hello world"); 4) b=new String("game over"); 5) System.out.println(a+b+"ok"); 6) a=null; 7) a=b; 8) System.out.println(a); 9) } In the absence of compiler optimization,which line is the earliest point the object a refered is definitely elibile to be garbage collected? A.3 B.5 C.6 D.7 E.9 3.In the class java.awt.AWTEvent,which is the parent class upon which jdk1.1 awt events are based there is a method called getID(), which phrase most accerrately describes in significance of the reture value of this method? A.It is a reference to the object directly affected by the cause of the event. B.It is an indication of the nature of the cause of the event. C.It is an indication of the position of the mouse when it caused the event. D.In the case of a mouse click,it is an indication of the text under the mouse at the time of the event. E.It tells the state of certain keys on the keybord at the time of the event. F.It is an indication of the time at which the event occurred.

4. Which statement about listener is true? A.Most component allow multiple listeners to be added. B.If mutiple listener be add to a single component,the event only affected one listener. C.Component don't allow multiple listeners to be add. D.The listener mechanism allows you to call an addListener method as many times as is needed,specifying as many different listeners as your design requires.

5.Give the following code: public class Example{ public static void main(String args[]){ int I=0; do{ System.out.println("Doing it for I is:"+ I ); }while(--i); System.out.println("Finish"); } } Which will be output: A.Doing it for I is 3 B.Doing it for I is 1 C.Doing it for I is 2 D.Doing it for I is 0 E.Doing if for I is -1 F.Finish

6.Give the code fragment: 1) switch(x){ 2) case 1: System.out.println("Test 1"); break; 3) case 2: 4) case 3:System.out.println("Test 2"); break; 5) default: System.out.println("end"); 6) }which value of x would cause "Test 2" to the output: A.1 B.2 C.3 D.default

7.Give incompleted method: 1) 2) { if(unsafe()) {//do something...} 3) else if(safe()){//do the other...} 4} } The method unsafe() will throw an IOException,which completes the method of

declaration when added at line one? A.public IOException methodName() B.public void methodName() C.public void methodName() throw IOException D.public void methodName() throws IOException F.public void methodName() throws Exception8.Give the code fragment:

8. if(x>4){ System.out.println("Test 1");} else if(x>9) { System.out.println("Test 2");} else { System.out.println("Test 3");} Which range of value of x would produce of output "Test 2"? A.x4 C.x>9 D.None

9.Give the following method: public void example(){ try{ unsafe(); System.out.println("Test 1"); }catch(SafeException e){System.out.println("Test 2"); }finally{System.out.println("Test 3");} System.out.println("Test 4"); } Which will display if method unsafe() run normally? A.Test 1 B.Test 2 C.Test 3 D.Test 4

10.Which method you define as the starting point of new thread in a class from which new the thread can be excution? A.public void start() B.public void run() C.public void int() D.public static void main(String args[]) E.public void runnable()

11.What wight cause the current thread to stop excutings? A.thread of higher priority become ready. B.method sleep() be called. C.method stop() be called D.method suspend() be called.

E.method wait() be called 12.Which modifier should be applied to a method for the lock of object this to be obtained prior to excution any of the mehod body? A.synchronized B.abstract C.final D.static E.public

13.The following code is entire contents of a file called Example.java,causes precisely one error during compilation: 1) class SubClass extends BaseClass{ 2) } 3) class BaseClass{ 4) String str; 5) public BaseClass(){ 6) System.out.println("ok");} 7) public BaseClass(String s){ 8) str=s; 9) public class Example{ 10) public void method(){ 11) SubClass s=new SubClass("hello"); 12) BaseClass b=new BaseClass("world"); 13) } 14) } 15) } 16) } Which line would be cause the error? A.9 B.10 C.11 D.12

14.Which statement is correctly declare a variable a which is suitable for refering to an array of 30 string empty object? A.String []a B.String a[] D.char a[][] E.String a[50] F.Object a[50]

15.Give the following java sourse fragement: //point x public class Interesting{ //do something } which statement is correctly Java syntax ant point x? A.import java.awt.*; B.package mypackage C.static int PI=3.14 D.public class MyClass{//do other thing...} E.class MyClass{//do something...}

16.Give this class outline: class Example{ private int x; //rest of class body... } Assuming that x invoked by the code java Example,which statement can made this.x be accessible in main() method of Example.java: A.Change private int x to public int x B.change private int x to static int x C.Change private int x to protected int x D.change private int x to final int x

17.The piece of preliminary analsis work describes a class that will be used frequently in many unrelated parts of a project: "The polygon object is a drawable,A polygon has vertex information stored in a vector,a color, length and width."Which Data type would be used? A.Vector B.int C.String D.Color E.Date

18.A class design requires that a member variable should be accessible only by same package,which modifer word should be used: A.protected B.public C.no modifer D.private 19.Which declares for a native method in a java class corrected? A.public native void method(){ } B.public native void method(); C.public native method(); D.public void method(){native;} E.public void native method(); 20.Which modifer should be applied to a declaration of a class member variable for the value of variable to remain constant after the creation of the object?(short answer)

21.Which is the main() method's return of a application?

A.String

B.byte

C.char

D.void

22.Which is corrected argument of main() method of a applicaton? A.String args B.String ar[] C.Char args[][] D.StringBuffer arg[]

23."The Employee object is a person,An Employee has appointment store in a vector,a hire date and a number of dependents." short anwser:use shortest statement define a class of Employee.

24.Give the following class defination inseparate source files: public class Example{ public Example(){//do something} protected Example(){// do something} protected void method(){//do something} } public class Hello extends Example{// member method and member variable} Which methods are corrected added to the class Hello? A.public void Example(){} B.public void method(){} C.protected void method(){} D.private void method() E.private otherMethod(){}

25.

Float s=new Float(0.9F); Float t=new Float(0.9F); Double u=new Double(0.9);Which expression's result is true? A.s==t; B.s.equal(t); C.s==u D.t.equal(u)

26.Give following class: class AClass{ val; public AClass(long v){val=v;} public static void main(String args[]){ AClass x=new AClass(10L);

private long

AClass y=new AClass(10L); AClass z=y; long a=10L; int b=10; } } Which expression's result is true? A.a= =b; B.a= =x; C.y= =z; =y; E.a= =10.0;

D.x=

27.A socket object has been created and connected to a standard internet sevice on a remote network server.Which construction give the most suitable means for reading ASCII data one line at a time from the socket? A.InputStream in=s.getInputStream(); B.DataInputStream in=new DataInputstream(s.getInputStream()); C.ByteArrayInputStream in=new ByteArrayInputStream(s.getInputStream()); D.BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream())); E.BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()),"8859-1");

28.String s="Example String"; Which operation is legal? A.s>>>=3; B.int =s.length(); C.s[3]="x"; D.String short s=s.trim(); E.String t="root"+s;

29.Give: String str="goodby"; Which operation is legal? A.char c=s[3]; B.int i=s.length(); C.s>>=2; D.String lower s=s.toLowerCase(); E.s+="long ago";

30.What use to position a Button in a Frame,width of Button is affected by the Frame size,which Layout Button will be set to? A.FlowLayout; B.GridLayout; C.North of

BorderLayout D.South of BorderLayout BorderLayout

D.Ease or West of

31.What use to position a Button in a Frame,size of Button is not affected by the Frame size,which Layout Button will be set to? A.FlowLayout; B.GridLayout; C.North of BorderLayout D.South of BorderLayout D.Ease or West of BorderLayout

32.An AWT GUI under exposure condition,which one or more method will be invok when it redraw? A.paint(); B.update(); C.repaint(); D.drawing();

33.Select valid identifier of Java: A.userName B.%passwd E.this

C.3d_game

D.$charge

34.Which are Java keyword? A.goto B.null C.FALSE D.native E.const

35.Run a corrected class:java -cs AClass a b c Which statement is true? A.args[0]="-cs"; B.args[1]="a b c"; C.args[0]="java"; D.args[0]="a"; E.args[1]='b';36.Give the following java class: public class Example{ static int x[]=new int[15]; public static void main(String args[]){

System.out.println(x[5]); } } Which statement is corrected? A.When compile,some error will occur. B.When run,some error will occur. C.Output is zearo. D.Output is null.37.Give the following java class: public class Example{ public static void main(String args[]){ static int x[]=new int[15]; System.out.println(x[5]); } } Which statement is corrected? A.When compile,some error will occur. B.When run,some error will occur. C.Output is zearo. D.Output is null.38. Short answer: The decimal value of i is 12,the octal i value is:39.short answer: The decimal value of i is 7,the hexadecimal is:

40.Which is the range of char? A.2-7~27-1 B.0~216-1

C.0~216

41.Which is the range of int type? A.2-16~2 16-1 B.2-31~ 2 31-1 D.2-64~2 64-1

D.0~28

C.2-32~2 32-1

42.Give the following class: public class Example{ String str=new String("good"); char ch[]={'a','b','c'}; public static void main(String args[]){ Example ex=new Example(); ex.change(ex.str,ex.ch); System.out.println(ex.str+" and "+ex.ch); } public void change(String str,char ch[]){ str="test ok";ch[0]="g"; } } Which is the output: A.good and abc B.good and gbc C.test ok and abc D.test ok and gbc

43.Which code fragments would correctly identify the number of arguments passed via command line to a Javaapplication, excluding the name of the class that is being invoked? A.int count = args.length; B.int count = args.length - l; C.int count=0; while (args [count] !=null) count ++; D.int count=0; while (!(args[count].equals(""))) count ++;

44.FilterOutputStream is the parent class for BufferedOutputStream, DataOutputStream and PrintStream. Which classes are a valid argument for the constructor of a FilterOutputStream? A.InputStream B.OutputStream C.File D.RandomAccessFile E.StreamTokenizer

45.Given a TextArea using a proportional pitch font and constructed like this: TextArea t = new TextArea("12345", 5, 5); Which statement is true? A.the displayed width shows exactly five characters one ach line unless otherwise constrained B.The displayed height is five lines unless otherwise constrained. C.The maximum number of characters in a line will be five. D.The user will be able to edit the character string E.The displayed string can use multiple fonts

46.Given a TextArea using a proportional pitch font and constructed like this: List l=new List(5,true); Which statement is true? A.the displayed item exactly five line unless otherwise constrained B.The displayed item is five lines init.but can displayed more than five Item by C.The maximum number of item in a list will be five. D.The list is multiple mode 47.Given this skeleton of a class currently under construction: public class Example { int x, y,z;

public Example(int a,int b) { //lots of complex computation x=a;y=b; } public Example(int a, int b,int c) { // do everything the same as single argument // version of constructor //including assignment x = a,y=b z=c; } } What is the most concise way to code the "do everything..."part of the constructor taking two arguments? (short answer)

48.Which correctly create a two dimensional array of integers? A.int a[][] = new int[10,10] B.int a[10][10] = new int[][]; C.int a[][] = new int [10][10]; D.int []a[] = new int [10][10]; E.int []a[] = new int[10][10];

49.Which are correct class declarations? Assume in each case that the text constitutes the entire contents of a file called Fred.java on a system with a case-significant file system. A.public class Fred { public int x = 0; public Fred (int x) { this.x = x; } } B.public class fred public int x = 0; public fred (int x) { this.x = x; } } C.public class Fred extends MyBaseClass, MyOtherBaseClass { public int x = 0; public Fred (int xval) { x = xval; } }

D.protected class Fred { private int x = 0; private Fred (int xval) { x = xval; } } E.import java.awt.*; public class Fred extends Object { int x; private Fred (int xval) { x = xval; } } 50.A class design requires that a particular member variable must be accessible for direct access by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this? A.The variable should be marked public B.The variable should be marked private C.The variable should be marked protected D.The variable should have no special access modifier E.The variable should be marked private and an accessor method provided 51.Which correctly create an array of five empty Strings? A.String a [] = new String [5]; for (int i = 0; i < 5; a[i++] = ""); B.String a [] = {"", "", "", "", ""}; C.String a [5]; D.String [5] a; E.String [] a = new String [5]; for (int i = 0; i < 5; a[i++] = null); 52.Which cannot be added to a Container? A.an Applet B.a Component C.a Container D.a MenuComponent E.a panel 53.Which is the return value of Event listener's method? A.String B.AWTEvent C.void D.int 54.If we implements MouseEventListener,which is corrected argument of it's method?(short answer) 55.Use the oprator ">>" and ">>>".Which statement is true? A.1010 0000 0000 0000 0000 0000 0000 0000>>4 give 0000 1010 0000 0000 0000 0000 0000 0000 B.1010 0000 0000 0000 0000 0000 0000 0000>>4 give 1111 1010 0000 0000 0000 0000 0000 0000 A.1010 0000 0000 0000 0000 0000 0000 0000>>>4 give 0000 1010 0000 0000 0000 0000 0000 0000 A.1010 0000 0000 0000 0000 0000 0000 0000>>>4 give 1111 1010 0000 0000 0000 0000 0000 000056.

56. Give following fragment. outer:for(int i=0;i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF