Core Java 19 topics Questions and Answers
/ * THREADS-0 */ 1Q.What will be the output of the program? class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); (); S("one. "); (); S("two. ");} public void run() { S("Thread "); }} ans : an exception occurs at runtime. 2Q.Which of the following statements can be used to create a new Thread? (Choose TWO) Implement .Thread and implement the start() method. Extend .Thread and override the run() method. ** Implement .Runnable and implement the run() method ** Extend .Runnable and override the start() method. Implement .Thread and implement the run() method 3Q. class PingPong2 { synchronized void hit(long n) { for(int i = 1; i 3; i++) S(n + "-" + i + " "); }} public class Tester implements Runnable { static PingPong2 pp2 = new PingPong2(); public static void main(String[] args) { new Thread(new Tester()).start(); new Thread(new Tester()).start();} public void run() { (TntThread().getId()); } } Which statement is true? The output could be 6-1 6-2 5-1 5-2 ** The output could be 6-1 6-2 5-1 7-1 The output could be 6-1 5-2 6-2 5-1 The output could be 5-1 6-1 6-2 5-2 4Q.Consider the following code and choose the correct option: class Nthread extends Thread{ public void run(){ S("Hi");} public static void main(String args[]){ Nthread th1=new Nthread(); Nthread th2=new Nthread();} will not create any child thread ** Will create two child threads and display Hi twice will display Hi once compilation error 5Q.What will be the output of the program? class MyThread extends Thread { MyThread() {} MyThread(Runnable r) {super(r); } public void run() { S("Inside Thread "); } } class MyRunnable implements Runnable { public void run() { S(" Inside Runnable"); } } class Test { public static void main(String[] args) { new MyThread().start(); new MyThread(new MyRunnable()).start(); } } Throws exception at runtime Prints "Inside Thread Inside Thread" ** Does not compile Prints "Inside Thread Inside Runnable" 6Q.Given: public class Threads4 { public static void main (String[] args) { new Threads4().go(); } public void go() { Runnable r = new Runnable() { public void run() { S("run"); } }; Thread t = new Thread(r); (); (); } } What is the result? The code executes normally, but nothing is printed. Compilation fails. The code executes normally and prints "run". An exception is thrown at runtime. ** 7Q.Consider the following code and choose the correct option: class Cthread extends Thread{ public void run(){ S("Hi");} public static void main (String args[]){ Cthread th1=new Cthread(); (); (); (); }} will print Hi Thrice ** will print Hi twice and throws Exception at run time will print Hi once Compilation error 8Q.class Cthread extends Thread{ public void run(){ S("Hi");} public static void main (String args[]){ Cthread th1=new Cthread(); (); (); (); }} will start two thread will print Hi twice and throws exception at runtime ** will not print will print Hi Once 9Q.Which of the following methods are defined in class Thread? (Choose TWO) run() ** start() ** terminate() wait() notify() 10Q.Assume the following method is properly synchronized and called from a thread A on an object B: wait(2000); After calling this method, when will the thread A become a candidate to get another turn at the CPU? Two seconds after lock B is released. Two seconds after thread A is notified. After thread A is notified, or after two seconds. ** After the lock on B is released, or after two seconds 11Q. public class MyRunnable implements Runnable { public void run() { // some code here } } which of these will create and start this thread? new Thread(new MyRunnable()).start(); ** new Thread(MyRunnable).run(); new Runnable(MyRunnable).start(); new MyRunnable().start(); 12Q.Consider the following code and choose the correct option: class Cthread extends Thread{ Cthread(){start();} public void run(){ S("Hi");} public static void main (String args[]){ Cthread th1=new Cthread(); Cthread th2=new Cthread(); }} compilation error will create two child threads and display Hi twice ** will not create any child thread will display Hi once 13Q. A) Exception is the superclass of all errors and exceptions in the java language B) RuntimeException and its subclasses are unchecked exception. Only A is TRUE Both A and B are FALSE Only B is TRUE ** Both A and B are TRUE 14Q.The following block of code creates a Thread using a Runnable target: Runnable target = new MyRunnable(); Thread myThread = new Thread(target); Which of the following classes can be used to create the target, so that the preceding code compiles correctly? public class MyRunnable extends Runnable{public void run(){}} public class MyRunnable extends Object{public void run(){}} public class MyRunnable implements Runnable{public void run(){}} ** public class MyRunnable implements Runnable{void run(){}} 15Q. A) Multiple processes share same memory location B) Switching from one thread to another is easier than switching from one process to another C) Thread makes it possible to maximize resource utilization D) Process is a light weight program All are FALSE Only B and C is TRUE ** Only A and B is TRUE Only C and D is TRUE 16Q.class Thread2 { public static void main(String[] args) { new Thread2().go(); } public void go(){ Runnable rn=new Runnable(){ public void run(){ Sln("Good Day.."); } }; Thread t=new Thread(rn); (); }} what should be the correct output for the code written above? An exception is thrown at runtime. Compilation fails. prints Good Day.. Twice The code executes normally and prints "Good Day.." ** 17Q. wait(), notify() and notifyAll() methods belong to Interrupt class Object class ** Thread class none of the listed options 18Q.Which of the following methods registers a thread in a thread scheduler? construct(); run(); register(); start();** /* THREADS-1 */ 1Q.Carefully read the question and answer accordingly. State whether TRUE or FALSE. The below code will compile & provide desired output: package p1; class MyThread extends Thread { public void run() { Sln("Important job running in MyThread"); } public void run(String s) { Sln("String in run is " + s); }} class Test { public static void main(String[] args) { MyThread t1=new MyThread(); (); }} FALSE TRUE *** 2. Carefully read the question and answer accordingly. Inter thread communication is achieved using which of the below methods? notify() notifyAll() all the options*** wait() 3. Carefully read the question and answer accordingly. Which of these is not a benefit of Multithreading? Requires less overheads compared to multitasking. Support parallel operation of functions. Increase system efficiency. None of the options.*** Reduce response time of process. 4. Carefully read the question and answer accordingly. class Background implements Runnable{ int i = 0; public int run(){ while (true) { i++; Sln("i="+i); } return 1; } }//End class It will compile and calling start will print out the increasing value of i. It will compile and the run method will print out the increasing value of i. Compilation will cause an error because while cannot take a parameter of true. The code will cause an error at compile time. *** 5. Carefully read the question and answer accordingly. Synchronization is achieved by using which of the below methods Synchronized blocks *** Synchronized abstract classes Synchronized classes Synchronized methods *** Synchronized interfaces 6. Carefully read the question and answer accordingly. public class TestDemo implements Runnable { public void run() { S("Runner"); } public static void main(String[] args) { Thread t = new Thread(new TestDemo()); (); (); (); } } What will be the result? You cannot call run() method using Thread class object. An exception is thrown at runtime. The code executes and prints "RunnerRunnerRunner". *** The code executes and prints "Runner". 7. Carefully read the question and answer accordingly. Which of the following statements are true? Statement1: When a thread is sleeping as a result of sleep(), it releases its locks. Statement2: The O() method can be invoked only from a synchronized context. BOTH Statement1 & Statement2 are FALSE. BOTH Statement1 & Statement2 are TRUE. Statement2 is TRUE but Statement1 is FALSE.*** Statement1 is TRUE but Statement2 is FALSE. 8. Carefully read the question and answer accordingly. What will be the output of below code: package p1; class MyThread extends Thread { public void run() { Sln("Important job running in MyThread"); } } class Test { public static void main(String[] args) { MyThread t1=new MyThread(); ();}} Runtime Exception Non of the options Compile Error Important job running in MyThread*** 9. Carefully read the question and answer accordingly. Which two statements are true? 1. It is possible for more than two threads to deadlock at once. 2. The JVM implementation guarantees that multiple threads cannot enter into a deadlocked state. 3. Deadlocked threads release once their sleep() method's sleep duration has expired. 4. If a piece of code is capable of deadlocking, you cannot eliminate the possibility of deadlocking by inserting invocations of T(). 2&4 2&3 1&2 1&4 *** 1&3 10. Carefully read the question and answer accordingly. Predict the output of below code: package p1; class MyThread extends Thread { public void run(int a) { Sln("Important job running in MyThread"); } public void run(String s) { Sln("String in run"); } } class Test { public static void main(String[] args) { MyThread t1=new MyThread(); (); } } Compile Error No Output *** Important job running in MyThread String in run Important job running in MyThread String in run 11. Carefully read the question and answer accordingly. Which of these is not valid method in Thread class void start() void run() boolean getPriority()*** boolean isAlive() 12. Carefully read the question and answer accordingly. State whether TRUE or FALSE. Threads are small process which run in shared memory space within a process. TRUE *** FALSE 13. Carefully read the question and answer accordingly. public class Threads { public static void main (String[] args) { new Threads().go(); } public void go() { Runnable r = new Runnable() { public void run() { S("Run"); } }; Thread t = new Thread(r); (); (); } } What will be the result? An exception is thrown at runtime.*** The code executes normally and prints "Run". Compilation fails. The code executes normally, but nothing is printed. 14. Carefully read the question and answer accordingly. Which of the below is invalid state of thread? Blocked Dead Runnable Stop *** Running 15. Carefully read the question and answer accordingly. You have created a TimeOut class as an extension of Thread, the purpose of which is to print a “Time’s Over” message if the Thread is not interrupted within 10 seconds of being started. Here is the run method that you have coded: public void run() { Sln(“Start!”); try { T(10000); Sln(“Time’s Over!”); } catch (InterruptedException e) { Sln(“Interrupted!”); } }Given that a program creates and starts a TimeOut object, which of the following statements is true? The delay between “Start!” being printed and “Time’s Over!” will be 10 seconds plus or minus one tick of the system clock. Exactly 10 seconds after the “Start!” is printed, “Time’s Over!” will be printed. Exactly 10 seconds after the start method is called, “Time’s Over!” will be printed. If “Time’s Over!” is printed, you can be sure that at least 10 seconds have elapsed since “Start!” was printed. *** /* Control Structures ,Wrapper class & Auto boxing -0 */ 1Q.What is the output : class One{ public static void main(String[] args) { int a=100; if(a10) Sln("M.S.Dhoni"); else if(a20) Sln("Sachin"); else if(a30) Sln("Virat Kohli");} } all of these Virat Kohli M.S.Dhoni *** M.S.Dhoni Sachin Virat Kohli 2Q.Which of the following statements is TRUE regarding a Java loop? If a variable of type int overflows during the execution of a loop, it will cause an exception A loop may have multiple exit points *** A continue statement doesn’t transfer control to the test statement of the for loop An overflow error can only occur in a loop 3Q.Consider the code below & select the correct ouput from the options: public class Test{ public static void main(String[] args) { String num=""; z: for(int x=0;x3;x++) for(int y=0;y2;y++){ if(x==1) break; if(x==2 && y==1) break z; num=num+x+y; }Sln(num);}} 0001 Compilation error *** 4Q.Choose TWO correct options: Subclasses of the class Reader are used to read character streams. *** To write characters to an outputstream, you have to make use of the classCharacterOutputStream. OutputStream is the abstract superclass of all classes that represent an outputstream of bytes. *** To write an object to a file, you use the class ObjectFileWriter 5Q.Given: int a = 5; int b = 5; int c = 5; if (a 3) if (b 4) if (c 5) c += 1; else c += 2; else c += 3; c += 4; What is the value of variable c after executing the following code? 9 5 11 *** 7 3 6Q.public class SwitchTest { public static void main(String[] args) { Sln("value =" + switchIt(4)); } public static int switchIt(int x) { int j = 1; switch (x) { case 1: j++; case 2: j++; case 3: j++; case 4: j++; case 5: j++; default: j++; } return j + x; } } What will be the output of the program? value = 2 value = 4 value = 8 *** value = 6 7Q.Given: public static void test(String str) { int check = 4; if (check = h()) { S(At(check -= 1) +", "); } else { S(At(0) + ", "); } } and the invocation: test("four"); test("tee"); test("to"); What is the result? r, t, t, An exception is thrown at runtime. r, e, o, Compilation fails. *** 8Q.Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ Long L = null; long l = L; Sln(L); Sln(l); }} 0 null Compilation error Null 0 Compiles but error at run time *** 9Q.Consider the following code and choose the correct output: class Test{ public static void main(String args[]){ int num=3; switch(num){ default :{ S("default");} case 1: case 3: case 4: { Sln("apple"); break;} case 2: case 5: { Sln("black berry"); }break; } }} compilation error default default apple apple *** 10Q.Given: public class Breaker2 { static String o = ""; public static void main(String[] args) { z: for(int x = 2; x 7; x++) { if(x==3) continue; if(x==5) break z; o = o + x; } Sln(o); } } What is the result? 24 *** 246 2 234 11Q.Which of these statements are true? ArrayList is a sub class of Vector Stack is a subclass of Vector *** LinkedList is a subclass of ArrayList HashTable is a sub class of Dictionary *** 12Q.What is the value of ’n’ after executing the following code? int n = 10; int p = n + 5; int q = p - 10; int r = 2 * (p - q); switch(n) { case p: n = n + 1; case q: n = n + 2; case r: n = n + 3; default: n = n + 4; } 14 28 Runtime Error Compilation Error *** 10 13Q.Examine the following code: int count = 1; while ( ) { S( count + " " ); count = count + 1; } Sln( ); What condition should be used so that the code prints: 1 2 3 4 5 6 7 8 count 9 *** count 8 count != 8 count+1 = 8 14Q.int I = 0; outer: while (true) { I++; inner: for (int j = 0; j 10; j++) { I += j; if (j == 3) continue inner; break outer; } continue outer; } Sln(I); What will be thr result? 3 4 2 1 *** 15Q.Consider the following code and choose the correct output: class Test{ public static void main(String args[]){ boolean flag=true; if(flag=false){ S("TRUE");}else{ S("FALSE");}}} compilation error false *** Compiles true 16Q.class AutoBox { public static void main(String args[]) { int i = 10; Integer iOb = 100; i = iOb; Sln(i + " " + iOb); } } whether this code work properly, if so what would be the result? No, Runtime error Yes, 100, 100 *** No, Compilation error Yes, 10, 100 17Q.Which statements are true about maps? (Choose TWO) The return type of the values() method is set All Map implementations keep the keys sorted Changes made in the Set view returned by keySet() will be reflected in the original map *** All keys in a map are unique *** The Map interface extends the Collection interface 18Q.what will be the result of attempting to compile and run the following class? Public class IFTest{ public static void main(String[] args){ int i=10; if(i==10) if(i10) Sln("a"); else Sln("b"); }} The code will compile correctly,but will not display any output The code will fail to compile because the syntax of the if statement is incorrect The code will compile correctly and display the letter b,when run *** The code will compile correctly and display the letter a,when run The code will fail to compile because the compiler will not be able to determine which if statement the else clause belongs to 19Q .Given: public class Batman { int squares = 81; public static void main(String[] args) { new Batman().go(); } void go() { incr(++squares); Sln(squares); } void incr(int squares) { squares += 10; } } What is the result? 81 92 82 *** 91 20Q.public void foo( boolean a, boolean b) { if( a ) { Sln("A"); /* Line 5 */ } else if(a && b) /* Line 7 */ { Sln( "A && B"); } else /* Line 11 */ { if ( !b ) { Sln( "notB") ; } else { Sln( "ELSE" ) ; } } } What would be the result? If a is false and b is true then the output is "ELSE" *** If a is false and b is false then the output is "ELSE" If a is true and b is false then the output is "notB" If a is true and b is true then the output is "A && B" 21Q.Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ String hexa = "0XFF"; int number = Ie(hexa); Sln(number); }} 255 *** Compiles but error at run time Compilation error 1515 22Q.Given: Float pi = new Float(3.14f); if (pi 3) { S("pi is bigger than 3. ");} else { S("pi is not bigger than 3. "); } finally { Sln("Have a nice day."); } What is the result? pi is bigger than 3. Have a nice day. pi is bigger than 3. An exception occurs at runtime. Compilation fails. *** 23Q.Which of the following statements about arrays is syntactically wrong? Person p[][] = new Person[2][]; Person p[5]; *** Person[] p []; Person[] p = new Person[5]; 24Q.What will be the output of the program? int x = 3; int y = 1; if (x = y) /* Line 3 */ { Sln("x =" + x); } Compilation fails. *** x = 1 x = 3 The code runs with no output. 25Q.Consider the following code and choose the correct output: public class Test{ public static void main(String[] args) { int x = 0; int y = 10; do { y--; ++x; } while (x 5); S(x + "," + y); } } 6,5 5,5 *** 5,6 6,6 26Q.Cosider the following code and choose the correct option: class Test{ public static void main(String args[]) { Sln(IInt("8", 10)); } } 2.E9 Compiles but no output Compilation error NumberFormatException at run time *** 27Q.Which of the following loop bodies DOES compute the product from 1 to 10 like (1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10)? int s = 1; for (int i = 1; i = 10; i++) { What to put here? } Compilation error s++; s += i * i; s *= i; *** s = s + s * i; 28Q.class Test{ public static void main(String[] args) { int x=-1,y=-1; if(++x=++y) Sln("R.T. Ponting"); else Sln("C.H. Gayle"); } } consider the code above & select the proper output from the options. Compile error *** C.H.Gayle R.T.Ponting none of the listed options 29Q.switch(x) { default: Sln("Hello"); } Which of the following are acceptable types for x? 5.Short 6.Long 4 and 6 1 ,3 and 5 *** 2 and 4 3 and 5 30Q.Given: public class Test { public enum Dogs {collie, harrier, shepherd}; public static void main(String [] args) { Dogs myDog = Derd; switch (myDog) { case collie: S("collie "); case default: S("retriever "); case harrier: S("harrier "); } } } What is the result? retriever harrier Compilation fails. *** shepherd 31Q.Given: int n = 10; switch(n) { case 10: n = n + 1; case 15: n = n + 2; case 20: n = n + 3; case 25: n = n + 4; case 30: n = n + 5; } Sln(n); What is the value of ’n’ after executing the following code? 25 Compilation Error 23 **** Runtine Error 32 32Q.Consider the following code and choose the correct output: class Test{ public static void main(String args[]){ int a=5; if(a=3){ S("Three");}else{ S("Five");}}} Compilation error ***** Five Compiles but no output Three 33Q.Which of the following statements are true regarding wrapper classes? (Choose TWO) String is the wrapper class of char Double has a compareTo() method *** String is a wrapper class Character has a intValue() method Byte extends Number *** 34Q.public class Test { public static void main(String [] args) { int x = 5; boolean b1 = true; boolean b2 = false; if ((x == 4) && !b2 ) S("1 "); S("2 "); if ((b2 = true) && b1 ) S("3 "); } } What is the result 3 2 3 *** 2 1 2 3 35Q.import .SortedSet; import .TreeSet; public class Main { public static void main(String[] args) { TreeSetString tSet = new TreeSetString(); tS("1"); tS("2"); tS("3"); tS("4"); tS("5"); SortedSet sortedSet = ("3"); Sln("Head Set Contains : " + sortedSet); } } What is the missing method in the code to get the head set of the tree set? headSet tSSet *** HeadSet et 36Q.Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ Long l=0l; Sln(s(0));}} false *** Compilation error 1 true 37Q.Given: public class Test { public enum Dogs {collie, harrier}; public static void main(String [] args) { Dogs myDog = De; switch (myDog) { case collie: S("collie "); case harrier: S("harrier "); } }} What is the result? Compilation fails. collie harrier *** harrier collie 38Q.Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ int x=034; int y=12; int ans=x+y; Sln(ans); }} Compiles but error at run time compilation error 40 *** 46 39Q.What will be the output of following code? import .*; class I { public static void main (String[] args) { Object i = new ArrayList().iterator(); S((i instanceof List)+","); S((i instanceof Iterator)+","); S(i instanceof ListIterator); } } Prints: false, false, false Prints: false, true, false *** Prints: false, true, true Prints: false, false, true 40Q.What are the thing to be placed to complete the code? class Wrap { public static void main(String args[]) { iOb = Integer(100); int i = iOValue(); Sln(i + " " + iOb); // displays 100 100 } } Int int, int int, Integer Integer, new *** Integer, int 41Q.What is the output : class Test{ public static void main(String[] args) { int a=5,b=10,c=1; if(ac){ Sln("success"); } else{ break; } } } compiler error *** none of the listed options success runtime error 42Q.Given: import .*; public class Explorer3 { public static void main(String[] args) { TreeSetInteger s = new TreeSetInteger(); TreeSetInteger subs = new TreeSetInteger(); for(int i = 606; i 613; i++) if(i%2 == 0) (i); subs = (TreeSet)Set(608, true, 611, true); (629); Sln(s + " " + subs); } } What is the result? Compilation fails. An exception is thrown at runtime. *** [608, 610, 612, 629] [608, 610, 629] [608, 610, 612, 629] [608, 610] 43Q.Given: public class Barn { public static void main(String[] args) { new Barn().go("hi", 1); new Barn().go("hi", "world", 2); } public void go(String... y, int x) { S(y[h - 1] + " "); } } What is the result? world world hi hi Compilation fails. ***** hi world 44Q.Given: static void myFunc() { int i, s = 0; for (int j = 0; j 7; j++) { i = 0; do { i++; s++; } while (i j); } Sln(s); } } What would be the result 23 24 22 **** 20 21 45Q.Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ Long L = null; long l = L; Sln(L); Sln(l); }} Compiles but error at run time ******* 0 null null 0 Compilation error 46Q.Which collection implementation is suitable for maintaining an ordered sequence of objects,when objects are frequently inserted in and removed from the middle of the sequence? LinkedList *** HashSet TreeMap Vector ArrayList 47Q.What is the output of the following code : class try1{ public static void main(String[] args) { Sln("good"); while(false){ Sln("morning"); } } } good morning morning …. good runtime error compiler error **** 48Q.What is the range of the random number r generated by the code below? int r = (int)(M(Mm() * 8)) + 2; 2 = r = 9 ***** 2= r = 10 3 = r = 9 3 = r = 10 49Q.Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ Long l=0l; Sln(s(0));}} false **** 1 Compilation error true /* Control Structures ,Wrappers & Autoboxing -1 */ 1.() Carefully read the question and answer accordingly. The result of 10.987+”30.765” is . 10.987 10.9873.765 Compilation error 10.98730.765 *** 2.() Carefully read the question and answer accordingly. is a multi way branch statement switch *** label branch continue break 3.() Carefully read the question and answer accordingly. Which of the following is a loop construct that will always be executed once? for for..each switch do …. While *** while 4.() Carefully read the question and answer accordingly. What is the number of bytes used by Java primitive long The number of bytes is compiler dependent 4 2 8 **** 64 5.() Carefully read the question and answer accordingly. Data can be passed to the function by reference none of these by value *** Both by value & reference 6.() Carefully read the question and answer accordingly. What will be the output for following code? public class Wrapper11 { public static void main(String[]args) { Long l=100; Sln(l); } } 100 runtime Exception code will execute with out printing Compilation error*** 7.() Carefully read the question and answer accordingly. What will be the output for following code? public class Wrapper2 { public static void main(String[]args){ Byte b=1; Byte a=2; Sln(a+b); } } Runtime Error compiles and prints 12 compilation error compiles and print 3 *** 8.() Carefully read the question and answer accordingly. The statement is used inside the switch to terminate a Statement sequence break *** goto exit Jump escape 9.() Carefully read the question and answer accordingly. What will be the output of below code? public class Test { public static void main(String[] args) { int i = 1; Integer I = new Integer(i); method(i); method(I); } static void method(Integer I) { S(" Wrapper"); } static void method(int i) { S(" Primitive"); } } Wrapper Primitive Primitive Wrapper **** None Of the options Wrapper Primitive 10.() Carefully read the question and answer accordingly. What will be the output for following code? public class WrapperClass1 { public static void main(String[]args){ String s="10Bangalore"; int i=IInt(s); Sln(i); } } 10 Runtime Exception **** 10Bangalore Compilation error 11.() Carefully read the question and answer accordingly. We can use Wrapper objects of type int, short, char in switch case. State True or False. TRUE *** FALSE 12.() Carefully read the question and answer accordingly. What will be the output for following code? public class WrapperClass1 { public static void main(String[]args){ Integer i=new Integer(10); Integer j=new Integer(10); Sln(i==j); } } True compilation error None of the listed options False **** 13.() Carefully read the question and answer accordingly. What will be the output for following code? public class While { public static void main(String[]args){ int a='A'; int i=a+32; while(a='Z'){ a++; } Sln(i); Sln(a); } } a,z A,Z 97,91 **** 91,97 14.() Carefully read the question and answer accordingly. Each case in switch statement should end with statement none break **** continue default new 15.() Carefully read the question and answer accordingly. What happens when the following code is compiled and run. Select the one correct answer. for(int i = 1; i 3; i++) for(int j = 3; j = 1; j--) assert i!=j : i; The class compiles and runs, but does not print anything. The program generates a compilation error. The number 2 gets printed with AssertionError The number 3 gets printed with AssertionError The number 1 gets printed with AssertionError*** 16.() Carefully read the question and answer accordingly. What is the value of variable "I" after execution of following code? public class Evaluate { public static void main(String[]args) { int i=10; if(((i++)12)&&(++i15)) Sln(i); else Sln(i); } } 10 None of the listed options 12 11 **** 18.() Carefully read the question and answer accordingly. What will be the output for following code? public class WrapperClass12 { public static void main(String[]args) { Boolean b=true; boolean a=BBoolean("tRUE"); Sln(b==a); } } True **** False Runtime Exception Compilation error /* Access Specifiers 0 and 1 */ 1. Which modifier is used to control access to critical code in multi-threaded programs? transient default synchronized *** public 2. What will happen if a main() method of a "testing" class tries to access a private instance variable of an object using dot notation? The compiler will automatically change the private variable to a public variable The compiler will find the error and will not make a .class file *** The program will compile and run successfully The program will compile successfully, but the .class file will not run correctly 3. class Order{ Order(){ Sln("Cat"); } public static void main(String... Args){ Order obj = new Order(); Sln("Ant"); } static{ Sln("Dog"); } { Sln("Man"); }} consider the code above & select the proper output from the options. compile error Cat Ant Dog Man Dog *** Man Cat Ant Man Dog Cat Ant 4. 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 h;i++) Sln(ar[i]); } } Compilation Error occurs and to avoid them we need to declare Mine class as abstract *** A Sequence of 5 zero's will be printed like 0 0 0 0 0 A Sequence of 5 one's will be printed like 1 1 1 1 1 IndexOutOfBoundes Error 5. Which of the following declarations are correct? (Choose TWO) String s = “null”; *** boolean b = TRUE; int i = new Integer(“56”); *** byte b = 256; 6. public class MyAr { public static void main(String argv[]) { MyAr m = new MyAr(); od(); } public void amethod() { final int i1; Sln(i1); } } What is the Output of the Program? Unresolved compilation problem *** The local variable i1 may not have been initialized 0 None of the given options Compilation and output of null 7. A constructor may return value including class type false true *** 8. Given: public class Yikes { public static void go(Long n) {S("Long ");} public static void go(Short n) {S("Short ");} public static void go(int n) {S("int ");} public static void main(String [] args) { short y = 6; long z = 7; go(y); go(z); } } What is the result? int Long *** Compilation fails. An exception is thrown at runtime. Short Long 9. Consider the following code and choose the correct option: class A{ private void display(){ S("Hi");} public static void main(String ar[]){ display();}} Compilation fails *** Compiles but doesn't display anything Compiles and throws run time exception Compiles and displays Hi 10. Consider the following code and choose the correct option: public class MyClass {public static void main(String arguments[]) {amethod(arguments);}public void amethod(String[] arguments){Sln(arguments[0]);Sln(arguments[1]);}} Command Line arguments - Hi, Hello Runtime Error Runs but no output Compiler Error *** prints Hi Hello 12. Consider the following code and choose the correct option: class Test{ private void display(){Sln("Display()");}private static void show() { display();Sln("show()");}public static void main(String arg[]){ show();}} Compilation error *** Compiles and prints show() Compiles and prints Display() show() Compiles but throws runtime exception 13. class One{ int var1; One (int x){ var1 = x; }} class Derived extends One{ int var2; Derived(){ super(10); var2=10; } void display(){ Sln("var1="+var1+" , var2="+var2); }} class Main{ public static void main(String[] args){ Derived obj = new Derived(); ay(); }} consider the code above & select the proper output from the options. 0,0 var1=10 , var2=10 *** runtime error compile error 14. Consider the following code and choose the correct option: package aj; private class S{ int roll;S(){roll=1;} }package aj; class T { public static void main(String ar[]){ S(new S().roll);}} Compiles and display 1 Compiles and diplay 0 Compiles but no output Compilation error *** 15. Consider the following code and choose the correct option: package aj; class S{ int roll =23; private S(){} } package aj; class T { public static void main(String ar[]){ S(new S().roll);}} Compilation error *** Compiles and display 0 Compiles but no output Compiles and display 23 16. Which of the following sentences is true? A) Access to data member depends on the scope of the class and the scope of data members B) Access to data member depends only on the scope of the data members C) Access to data member depends on the scope of the method from where it is accessed Only A is TRUE Only A and C is TRUE All are TRUE *** All are FALSE 17. Consider the following code and choose the best option: class Super{ int x; Super(){x=2;}} class Sub extends Super { void displayX(){ S(x);} public static void main(String args[]){ new Sub().displayX();}} Compiles and display 0 Compilation error Compiles and display 2 *** Compiles and runs without any output 18. public class MyAr { public static void main(String argv[]) { MyAr m = new MyAr(); od(); } public void amethod() { static int i1; Sln(i1); } } What is the Output of the Program? It is not possible to declare a static variable in side of non static method or instance method. Because Static variables are class level dependencies. *** Compile time error because i has not been initialized Compilation and output of null 0 19. public class Q { public static void main(String argv[]) { int anar[] = new int[] { 1, 2, 3 }; Sln(anar[1]); } } Compiler Error: size of array must be defined Compiler Error: anar is referenced before it is initialized 2*** 1 20. Which statements, when inserted at (1), will not result in compile-time errors? public class ThisUsage { int planets; static int suns; public void gaze() { int i; // (1) INSERT STATEMENT HERE } } this = new ThisUsage(); = planets; ** i = ; ** i = ts; ** this.i = 4; 21. public class MyClass { static void print(String s, int i) { Sln("String: " + s + ", int: " + i); } static void print(int i, String s) { Sln("int: " + i + ", String: " + s); } public static void main(String[] args) { print("String first", 11); print(99, "Int first"); } }What would be the output? String: String first, int: 11 *** int: 99, String: Int first Compilation Error Runtime Exception int: 27, String: Int first String: String first, int: 27 23. What will happen when you attempt to compile and run this code? abstract class Base{ abstract public void myfunc(); public void another(){ Sln("Another method"); } } public class Abs extends Base{ public static void main(String argv[]){ Abs a = new Abs(); od(); } public void myfunc(){ Sln("My Func"); } public void amethod(){ myfunc(); } } The compiler will complain that the method myfunc in the base class has no body, nobody at all to print it The code will compile and run, printing out the words "My Func" *** The compiler will complain that the Base class has non abstract methods The code will compile but complain at run time that the Base class has non abstract methods 24. public class c1 {private c1(){ Sln("Hello");}public static void main(String args[]){c1 o1=new c1();}} What is the output? It is not possible to declare a constructor private Compilation Error Hello *** Can't create object because constructor is private 25. What will be the result when you attempt to compile this program? public class Rand{ public static void main(String argv[]){ int iRand; iRand = Mm(); Sln(iRand); } } A compile time error as random being an undefined method A random number between 1 and 10 Compile time error referring to a cast problem *** A random number between 0 and 1 26. Carefully read the question and answer accordingly. What will be the output for following code? class Super { static void show() { Sln("super class show method"); } static class StaticMethods { void show() { Sln("sub class show method"); } } public static void main(String[]args) { S(); new Super.StaticMethods().show(); } } super class show method sub class show method *** super class show method Compilation Error sub class show method super class show method 27. Carefully read the question and answer accordingly. Which of the following are true about constructors? Constructor is a special type of method which may have return type. Constructors can be overloaded ** Constructors can be overridden. Constructors should be called explicitly like methods 28. Carefully read the question and answer accordingly. Which of the following statements are true about Method Overriding? I: Signature must be same including return type II: If the super class method is throwing the exception then overriding method should throw the same Exception III: Overriding can be done in same class IV: Overriding should be done in two different classes with no relation between the classes III II & IV I *** I & III 29. Carefully read the question and answer accordingly. If display method in super class has a protected specifier then what should be the specifier for the overriding display method in sub class? None of the listed options protected or default private protected or public *** 30. When one method is overridden in sub class the access specifier of the method in sub class should be equal as method in super class. State True or False. TRUE FALSE *** 31. Carefully read the question and answer accordingly. A field with default access specifier can be accessed out side the package. State True or False. FALSE ** TRUE 32. determines which member of a class can be used by other classes. Implementation Class specifier Inheritance Access specifier ** 33. Carefully read the question and answer accordingly. Constructor of an class is executed each time when an object of that class is created FALSE TRUE *** 34. Carefully read the question and answer accordingly. What will be the output of following code? class Super2 { public void display() { Sln("super class display method"); } public void exe() { Sln("super class exe method"); display(); } } public class InheritMethod extends Super2 { public void display() { Sln("sub class display method"); } public static void main(String [] args) { InheritMethod o=new InheritMethod(); (); } } super class exe method super class display method None of the listed options super class exe method sub class display method .*** Compilation error 35. Carefully read the question and answer accordingly. What will be the output for following code? class Super { int num=20; public void display() { Sln("super class method"); } } public class ThisUse extends Super { int num; public ThisUse(int num) { =num; } public void display() { Sln("display method"); } public void Show() { ay(); display(); Sln(); Sln(num); } public static void main(String[]args) { ThisUse o=new ThisUse(10); o.Show(); } } display method display method 20 20 super class method display method 10 10 display method display method 10 10 *** super class method display method 20 20 38. The constructor of a class must not have a return type. TRUE FALSE **** 41.A class can be declared as if you do not want the class to be subclassed. Using the keyword we can abstract a class from its implementation public,friend final,protected private,abstract protected ,interface final,interface *** 44.Carefully read the question and answer accordingly. Which of the following are true about protected access specifier? A class can be declared as protected. If one class is having protected method then the method is available for subclass which is present in another package *** All members of abstract class are by default protected Protected is default access modifier of a child class 47. Carefully read the question and answer accordingly. Which of the following method is used to initialize the instance variable of a class. Class Variable Destructor Public Constructor *** 1) Given: package QB; class Meal { Meal() { Sln("Meal()"); } } class Cheese { Cheese() { Sln("Cheese()"); } } class Lunch extends Meal { Lunch() { Sln("Lunch()"); } } class PortableLunch extends Lunch { PortableLunch() { Sln("PortableLunch()"); } } class Sandwich extends PortableLunch { private Cheese c = new Cheese(); public Sandwich() { Sln("Sandwich()"); } } public class MyClass7 { public static void main(String[] args) { new Sandwich(); } } What would be the output? a) Meal() Lunch() PortableLunch() Sandwich() Cheese() b)Cheese() Sandwich() Meal() Lunch() PortableLunch() c) Meal() Cheese() Lunch() PortableLunch() Sandwich() d) Meal() *** Lunch() PortableLunch() Cheese() Sandwich() 2) Here is the general syntax for method definition: accessModifier returnType methodName( parameterList ) { Java statements return returnValue; } What is true for the returnType and the returnValue? a) The returnValue can be any type, but will be automatically converted to returnType when the method returns to the caller b) The returnValue must be the same type as the returnType, or be of a type that can be converted to returnType without loss of information *** c) The returnValue must be exactly the same type as the returnType. d) If the returnType is void then the returnValue can be any type 3) What will be the result of compiling the following program? public class MyClass { long var; public void MyClass(long param) { var = param; } // (Line no 1) public static void main(String[] args) { MyClass a, b; a = new MyClass(); // (Line no 2) } } a) A compilation error will occur at (2), since the class does not have a default constructor b) The program will compile without errors. *** c) A compilation error will occur at (Line no 2), since the class does not have a constructor that takes one argument of type int. d) A compilation error will occur at (Line no 1), since constructors cannot specify a return value 4) Consider the following code and choose the correct option: class A{ private static void display(){ S("Hi");} public static void main(String ar[]){ display();}} a) Compiles but doesn't display anything b) Compiles and throw run time exception c) Compiles and display Hi *** d) Compilation fails 5)Q. A) No argument constructor is provided to all Java classes by default B) No argument constructor is provided to the class only when no constructor is defined. C) Constructor can have another class object as an argument D) Access specifiers are not applicable to Constructor Only A is TRUE All are FALSE All are TRUE *** B and C is TRUE 6) What will be printed out if you attempt to compile and run the following code ? public class AA { public static void main(String[] args) { int i = 9; switch (i) { default: Sln("default"); case 0: Sln("zero"); break; case 1: Sln("one"); case 2: Sln("two"); } } } a) Compilation Error b) default *** zero c) default zero one two d) default 7) Suppose class B is sub class of class A: A) If class A doesn't have any constructor, then class B also must not have any constructor B) If class A has parameterized constructor, then class B can have default as well as parameterized constructor C) If class A has parameterized constructor then call to class A constructor should be made explicitly by constructor of class B Only A is TRUE Only A and C is TRUE Only B and C is TRUE *** All are FALSE 8)Q. 11. class Mud { 12. // insert code here 13. Sln("hi"); 14. } 15. } And the following five fragments: public static void main(String...a) { public static void main(String.* a) { public static void main(String... a) { public static void main(String[]... a) { public static void main(String...[] a) { How many of the code fragments, inserted independently at line 12, compile? 1 2 *** 3 0 10)Q. A) A call to instance method can not be made from static context. B) A call to static method can be made from non static context. Only B is TRUE Both are TRUE *** Only A is TRUE Both are FALSE 12)What will be the result when you try to compile and run the following code? private class Base{ Base(){ int i = 100; Sln(i); } } public class Pri extends Base{ static int i = 200; public static void main(String argv[]){ Pri p = new Pri(); Sln(i); } } Compile time error **** 100 200 100 followed by 200 12) Consider the following code and choose the correct option: class A{ int z; A(int x){z=x;} } class B extends A{ public static void main(String arg){ new B();}} Compiles and displays nothing None of the listed options Compilation error *** Compiles but throws run time exception 13) class Sample {int a,b; Sample() { a=1; b=2; Sln(a+"t"+b); } Sample(int x) { this(10,20); a=b=x; Sln(a+"t"+b); } Sample(int a,int b) { this(); this.a=a; this.b=b; Sln(a+"t"+b); } } class This2 { public static void main(String args[]) { Sample s1=new Sample (100); } } What is the Output of the Program? 1 2 100 100 10 20 1 2 *** 10 20 100 100 10 20 1 2 100 100 100 100 1 2 10 20 14) package QB; class Sphere { protected int methodRadius(int r) { Sln("Radious is: "+r); return 0; } } package QB; public class MyClass { public static void main(String[] args) { double x = 0.89; Sphere sp = new Sphere(); // Some code missing } } to get the radius value what is the code of line to be added ? SdRadius(); methodRadius(x); Nothing to add dRadius(x); *** 16)Carefully read the question and answer accordingly. A field with default access specifier can be accessed out side the package. State True or False. FALSE *** TRUE 18) Carefully read the question and answer accordingly. The constructor of a class must not have a return type. FALSE TRUE *** 19) Carefully read the question and answer accordingly. determines which member of a class can be used by other classes..3 Class Implementation Inheritance specifier Access specifier *** 20) Which modifier indicates that the variable might be modified asynchronously, so that all threads will get the correct value of the variable. transient volatile *** synchronized default 21) class Test{ static void method(){ ay(); } static display(){ Sln(("hello"); } public static void main(String[] args){ new Test().method(); } } consider the code above & select the proper output from the options. does not compile *** compiles but no output Runtime Error hello 22) Consider the following code and choose the correct option: package aj; class A{ protected int j; } package bj; class B extends A { public static void main(String ar[]){ S(new A().j=23);}} code compiles fine and will display 23 code compiles but will not display output j can not be initialized compliation error *** 23) class A, B and C are in multilevel inheritance hierarchy repectively . In the main method of some other class if class C object is created, in what sequence the three constructors execute? Constructor of C executes first followed by the constructor of A and B Constructor of A executes first, followed by the constructor of B and C Constructor of C executes first followed by the constructor of B and A *** Constructor of A executes first followed by the constructor of C and B 24) Consider the following code and choose the correct option: class A{ int a; A(int a){a=4;}} class B extends A{ B(){super(3);} void displayA(){ S(a);} public static void main(String args[]){ new B().displayA();}} Compiles and display 3 compilation error Compiles and display 4 compiles and display 0 *** 25) class A { int i, j; A(int a, int b) { i = a; j = b; } void show() { Sln("i and j: " + i + " " + j); } } class B extends A { int k; B(int a, int b, int c) { super(a, b); k = c; } void show(String msg) { Sln(msg + k); } } class Override { public static void main(String args[]) { B subOb = new B(3, 5, 7); subO("This is k: "); // this calls show() in B subO(); // this calls show() in A } } What would be the ouput? This is i: 7 j and k: 3 5 This is i: 3 j and k: 5 7 This is k: 7 *** i and j: 3 7 This is j: 5 i and k: 3 7 26) class One{ int var1; One (int x){ var1 = x; }} class Derived extends One{ int var2; void display(){ Sln("var 1="+var1+"var2="+var2); }} class Main{ public static void main(String[] args){ Derived obj = new Derived(); ay(); }} consider the code above & select the proper output from the options. 0 , 0 compile error *** compiles successfully but runtime error none of these 28)Given the following code what will be output? public class Pass{ static int j=20; public static void main(String argv[]){ int i=10; Pass p = new Pass(); od(i); Sln(i); Sln(j); } public void amethod(int x){ x=x*2; j=j*2; } } Error: amethod parameter does not match variable 10, and 20 10 and 40 *** 20 and 40 Q.class MyClass1 { private int area(int side) { return(side * side); } public static void main(String args[ ]) { MyClass1 MC = new MyClass1( ); int area = MC.area(50); Sln(area); } } What would be the output? 50 2500*** Compilation error Runtime Exception public class c123 { private c123() { Sln("Hellow"); } public static void main(String args[]) { c123 o1 = new c123(); c213 o2 = new c213(); } } class c213 { private c213() { Sln("Hello123"); } } What is the output? Hellow Compilation Error*** It is not possible to declare a constructor as private Runs without any output Consider the following code and choose the correct option: class A{ A(){S("From A");}} class B extends A{ B(int z){z=2;} public static void main(String args[]){ new B(3);}} Compilation error Comiples and prints From A *** Compiles but throws runtime exception Compiles and display 3 Which of the following will print -4.0 Sln(M(-4.7)); Sln(M(-4.7)); Sln(M(-4.7));*** Sln(M(-4.7)); Q.Consider the following code and choose the correct option: class Test{ private static void display(){ Sln("Display()");} private static void show() { display(); Sln("show()");} public static void main(String arg[]){ show();}} Compiles but throws runtime exception Compilation error Compiles and prints*** Display() show() Compiles and prints show() /* Keywords,varaiable,operands -0 */ 1. What will be the result of the following program? public class Init { String title; boolean published; static int total; static double maxPrice; public static void main(String[] args) { Init initMe = new Init(); double price; if (true) price = 100.00; Sln("|" + initM + "|" + initMshed + "|" + I + "|" + IPrice + "|" + price+ "|"); } } Compilation error The program will compile, and print | |false|0|0.0|0.0|, when run The program will compile, and print |null|true|0|0.0|100.0|, when run The program will compile, and print |null|false|0|0.0|100.0|, when run ** The program will compile, and print |null|false|0|0.0|0.0|, when run 2. Which of the following lines of code 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; Line 1, Line 3, Line 5 Line 5 ** Line 3 Line 4 Line 1, Line 5 3. Consider the code below & select the correct ouput from the options: class Test{ public static void main(String[] args) { parse("Four"); } static void parse(String s){ try { double d=DDouble(s); }catch(NumberFormatException nfe){ d=0.0; }finally{ Sln(d); } }} A ParseException is thrown by the parse method at runtime 0 Compilation error ** A NumberFormatException is thrown by the parse method at runtime 4. Which of the following are correct variable names? (Choose TWO) int #ss; int _; ** int $abc; ** int 1ah; 5. Given class MybitShift { public static void main(String [] args) { int a = 0x; S(a + " and "); a = a 25; Sln(a); } } 2 and - and 2 ** 2 and and -2 6. Identify the statements that are correct: (A) int a = 13, a2 = 3 (B) int b = -8, b1 = -4 (C) int a = 13, a2 = 3 (D) int b = -8, b1 = -4 (A), (B) & (C) ** (C) & (D) (A), (B), (C) & (D) (A) & (B) 7. Given the following piece of code: public class Test { public static void main(String args[]) { int i = 0, j = 5 ; for( ; (i 3) && (j++ 10) ; i++ ) { S(" " + i + " " + j ); } S(" " + i + " " + j ); } } what will be the output? 0 6 1 7 2 8 3 9 compilation fails 0 6 1 7 2 8 3 8 ** 0 5 1 5 2 5 3 5 8. What will be the output of the program ? public class Test { public static void main(String [] args) { signed int x = 10; for (int y=0; y5; y++, x--) S(x + ", "); } } 9, 8, 7, 6, 5, Compilation fails ** (The word "signed" is not a valid modifier keyword in the Java language.) An exception is thrown at runtime 10, 9, 8, 7, 6, 9. Consider the following code and choose the correct option: class Test{ class A{ static int x=3; } static void display(){ Sln(A.x); } public static void main(String[] args) { display(); }} Compiles but error at run time Compilation error ** 0 3 10. State the class relationship that is being implemented by the following code: class Employee { private int empid; private String ename; public double getBonus() { Accounts acc = new Accounts(); return lateBonus(); } } class Accounts { public double calculateBonus(){//method's code} } Composition Aggregation Dependency ** Simple Association 11. Which of the given options is similar to the following code: value += sum++ ; value = value + ++sum; value = value + sum; ** sum = sum + 1; sum = sum + 1; value = value + sum; value = value + sum; 12. Say that class Rodent has a child class Rat and another child class Mouse. Class Mouse has a child class PocketMouse. Examine the following Rodent rod; Rat rat = new Rat(); Mouse mos = new Mouse(); PocketMouse pkt = new PocketMouse(); Which one of the following will cause a compiler error? pkt = null pkt = rat ** rod = mos rod = rat 13.1. public class LineUp { 2. public static void main(String[] args) { 3. double d = 12.345; 4. // insert code here 5. } 6. } Which code fragment, inserted at line 4, produces the output | 12.345|? A. Sf("|%7f| n", d); B. Sf("|%3.7f| n", d); C. Sf("|%7.3d| n", d); D. Sf("|%7.3f| n", d); C A B D ** 14. Consider the following code snippet: int i = 10; int n = ++i%5; What are the values of i and n after the code is executed? 10, 0 11, 1 ** 11 , 0 10, 1 15. What is the output of the following: int a = 0; int b = 10; a = --b ; Sln("a: " + a + " b: " + b ); a: 9 b:11 a: 0 b:9 a: 10 b: 9 a: 9 b:9 ** 16. What will be the output of the program? public class CommandArgs { public static void main(String [] args) { String s1 = args[1]; String s2 = args[2]; String s3 = args[3]; String s4 = args[4]; S(" args[2] = " + s2); } } and the command-line invocation is java CommandArgs 1 2 3 4 args[2] = 2 args[2] = 3 args[2] = null An exception is thrown at runtime **(An exception is thrown because in the code String s4 = args[4];, the array index (the fifth element) is out of bounds. The exception thrown is the cleverly named ArrayIndexOutOfBoundsException.) 17. Consider the code below & select the correct ouput from the options: class A{ public int a=7; public void add(){ this.a+=2; S("a"); }} public class Test extends A{ public int a=2; public void add(){ this.a+=2; S("t"); } public static void main(String[] args) { A a =new Test(); (); S(a.a); }} t 7 ** t 9 a 9 Compilation error 18. What is the value of y when the code below is executed? int a = 4; int b = (int)M(a % 3 + a / 3.0); 4 2 3 ** 1 19. Consider the following code: int x, y, z; y = 1; z = 5; x = 0 - (++y) + z++; After execution of this, what will be the values of x, y and z? x = 4, y = 2, z = 6 x = -7, y = 1, z = 5 x = 3, y = 2, z = 6 ** x = 4, y = 1, z = 5 20. Consider the following code and choose the correct option: class Test{ interface Y{ void display(); } public static void main(String[] args) { Y y=new Y(){ public void display(){ Sln("Hello World"); } }; ay(); }} Compiles but error at run time Compiles but run without output Compilation error Hello World ** 21. What will be the result of the following program? public class Init { String title; boolean published; static int total; static double maxPrice; public static void main(String[] args) { Init initMe = new Init(); double price; if (true) price = 100.00; Sln("|" + initM + "|" + initMshed + "|" + I + "|" + IPrice + "|" + price+ "|"); } } The program will compile, and print |null|true|0|0.0|100.0|, when run The program will compile, and print |null|false|0|0.0|0.0|, when run The program will compile, and print |null|false|0|0.0|100.0|, when run ** Compilation error The program will compile, and print | |false|0|0.0|0.0|, when run 22. class C{ public static void main (String[] args) { byte b1=33; //1 b1++; //2 byte b2=55; //3 b2=b1+1; //4 Sln(b1+""+b2); }} Consider the code above & select the correct output. runtime exception prints 34,56 compile time error at line 4 ** compile time error at line 2 none of the listed options 23. Consider the following code snippet: int i = 10; int n = ++i%5; What are the values of i and n after the code is executed? 11, 1 ** 11 , 0 10, 1 10, 0 24. What is the value of y when the code below is executed? int a = 4; int b = (int)M(a % 3 + a / 3.0); 4 3 ** 2 1 25. Consider the following code and choose the correct option: class Test{ interface Y{ void display(); } public static void main(String[] args) { new Y(){ public void display(){ Sln("Hello World"); } }.display(); }} Compiles but run without output ** Compiles but error at run time Compilation error Hello World 26. Consider the following code and choose the correct option: class Test{ static class A{ interface X{ int z=4; } } static void display(){ Sln(A.X.z); } public static void main(String[] args) { display(); }} Compilation error 0 Compiles but error at run time 4 ** 27. Consider the following code and choose the correct output: int value = 0; int count = 1; value = count++ ; Sln("value: "+ value + " count: " + count); value: 1 count: 2 ** value: 1 count: 1 value: 0 count: 0 value: 0 count: 1 28. Consider the code below & select the correct ouput from the options: public class Test { public static void main(String[] args) { String[] elements = { "for", "tea", "too" }; String first = (h 0) ?elements[0] : null; Sln(first); }} The variable first is set to elements[0]. ** Compilation error Compiles but error at runtime The variable first is set to null. 29. Which of the following lines of code 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; Line 3 Line 5 ** Line 4 Line 1, Line 5 Line 1, Line 3, Line 5 30. What will happen if you attempt to compile and run the following code? Integer ten=new Integer(10); Long nine=new Long (9); Sln(ten + nine); int i=1; Sln(i + ten); Compile time error 19 followed by 11 ** 19 follwed by 20 10 followed by 1 31. Here is the general syntax for method definition: accessModifier returnType methodName( parameterList ) { Java statements return returnValue; } What is true for the returnType and the returnValue? The returnValue can be any type, but will be automatically converted to returnType when the method returns to the caller. If the returnType is void then the returnValue can be any type The returnValue must be exactly the same type as the returnType The returnValue must be the same type as the returnType, or be of a type that can be converted to returnType without loss of information. *** 32. Consider the code below & select the correct ouput from the options: public class Test { int squares = 81; public static void main(String[] args) { new Test().go(); } void go() { incr(++squares); Sln(squares); } void incr(int squares) { squares += 10; } } 92 82 ** Compilation error 91 33. Consider the code below & select the correct ouput from the options: public class Test { public static void main(String [] args) { int x = 5; boolean b1 = true; boolean b2 = false; if ((x == 4) && !b2 ) S("1 "); S("2 "); if ((b2 = true) && b1 ) S("3 "); } 1 3 2 3 ** 3 2 34. What is the output of the following: int a = 0; int b = 10; a = --b ; Sln("a: " + a + " b: " + b ); a: 9 b:9 ** a: 0 b:9 a: 10 b: 9 a: 9 b:11 35. As per the following code fragment, what is the value of a? String s; int a; s = "Foolish boy."; a = Of("fool"); -1 ** 4 random
Información del documento
- Subido en
- 24 de octubre de 2022
- Número de páginas
- 257
- Escrito en
- 2022/2023
- Tipo
- Examen
- Contiene
- Preguntas y respuestas