0% found this document useful (0 votes)
12 views132 pages

Java Assignment Full

The document contains a series of Java programming questions and code snippets, focusing on concepts such as constructors, method overriding, variable hiding, and bitwise operations. It includes explanations of expected outputs and potential errors for each code example. The document is structured as a quiz or study guide for Java programming, highlighting key principles and common pitfalls.

Uploaded by

sahaaditya.54321
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
12 views132 pages

Java Assignment Full

The document contains a series of Java programming questions and code snippets, focusing on concepts such as constructors, method overriding, variable hiding, and bitwise operations. It includes explanations of expected outputs and potential errors for each code example. The document is structured as a quiz or study guide for Java programming, highlighting key principles and common pitfalls.

Uploaded by

sahaaditya.54321
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
\Users\user\Desktop\Test>javac Test-java \Users\user\Desktop\Test>java Test public class Test { 20 public static void main(string[] args) { : 5 int x = 2, y = 3, 25 2 4 boolean bi 2 ste 32 bit representation 6 | 2-> e29e0e00 eeee9e00 caee9e00 eoo00a10 \Users\user\Desktop\Test> 3 -> — @eeeee8e eae09000 29000000 00000011 22900099 oee90000 09000000 99000010 -> 2 e2eeeeee eeeaeeee ea000000 oag00011 -> 3 2 2 = x & yj // Bitwise AND Operator a [Link]-printIn(z); // op: 2 4 x 33 // Shorthand Bitwise AND Operator 15 [Link] println(x); // 0p: 2 16 x |= 33 // Shorthand Bitwise OR Operator 17 [Link](x)3 // 0p: 3 18 b= x < y5 // Relational(or Less than Equal To)operator [Link](b); // true 1 Option: D is correct Week 1 - Day 1 What is the expected output? 1 freee Demo { 2 private Demo(int n){ 5) [Link]("int-arg"); 4 3 5 public static Demo(int n){ 6 [Link]("static int-arg"); iY } 8A public static void main(String[] args){ 9 Demo d = new Demo(12); 10 } aa ly Options: A. int-arg B. static int-arg Cc. int-arg static int-arg D. Compilation error at line 2 E. Compilation error at line 5 Week 1 - Day2 - Question 1 Given the following piece of code: 1 class Sample1 { 2 public int x = 5; 3} 4 5 =class Sample2 { 6 public void m1(int x, Sample1 s1){ 7 x = 103 8 S1.x = 20; 9 } 10 5 public static void main(String[] args){ a1 int x = @3 12 Sample1 s1 = new Sample1(); 13 new Sample2().m1(x, 51); 14 [Link](x + “ “ + s1.x); 15 } 16 |} Predict the output: ALe@5 B. @ 20 c. 105 D. 10 20 E. 100 Explnation: 1. The only applicable modifiers to constructor are public, protected, private and 2. Constructor ds a reusable block of code, which means we can call it from its subclasses during the creation of the objects. But, when we declare it as static, it can not be used by its subclasses. Static method, static variable, ete. can not be inherited from their subclasses because they belong to the class in which they are declared. class Parent { Parent() { [Link]( "Parent Constructor"); ; , Kc:\Users\user\Desktop\Test>javac [Link] class Child extends Parent { child() { [Link]( "subclass Constructor } public static void main(string args[]) { Child obj = new child(); } 2 lihen an object of Child is created then Parent constructor is called by Child constructor through constructor chaining. But if we allow static constructor in java and make Parent constructor static then it can’t be called by Child as above said static it is accessible within the class but not by the Childclass. That's why static and constructor are illegal combination. Predict the output: aos B. @ 20 [Correct] ces D. 19 20 £100 Explanation: In main() method we are creating a local variable x and initializing it with 0, and at line 12 a Sanplei class object 21 ds created and at that tine the instance variable x(meant ¢1.x) has the value [Link] line 13 a nameless object of Line 12 21 is a ce Sanple2 class is created and it's mi(-,~) is called with local variable x and si. Due to this s1 object reference is passed to method mi()and it is pointed by si which is a local variable of mi(). When at line 8 s1.x = 20; is executed ‘the previously assigned value of s1.x which is 5 will be overwritten by 20. Now when we passed the value x to mi() at Line 13 the local variable x of mi() gets the value 0. Next at line 7 that local variable x is with value 10, but as local variable x only has a scope upto mi(), so the value 10 is not reflected or updated in main() and local variable x of main() remains @. Thus Option B is correct. Week 1 - Day2 - Question 2 Given the following piece of code: 1 &class Test { 2 Test(){ 3 [Link].print1n("Hello"); ca. 5 public void m1(){ 6 this(); zu [Link]("Hi"); 8 } > 10 11 Sclass Main{ 12 5 public static void main(String[] args){ 13 Test obj = new Test(); 14 obj.m1(); 15 } fie} Predict the output: A. Hello Hello Hi B. Hello Hi Cc. Hello Hello RuntimeException D. Compilation Error E. Runtime Error (Due to object creation) Predict the output A. Hello Helle B. Hello HL c. Hello Hetlo Runtimeexception . Compilation Error [Correct] E. Runtime Error (Due to object creation) Explanation Constructor can be called from another constructor by this constructor and a constructor can't be called from any. method with this() constructor, as this() constructor can be used to call current class constructor, and it must be first Line inside of a cunstructor only, If wo uant to call a constructor inside fron a method we have to use, new Test); instead of this(); class Test { Test(){ [Link]("Hello"); > public void mi(){ new Test(); [Link]("i")5 ve Week 1 - Day 3 - Question 1 Consider the following coding snippet: 1 Helass A { 25 AQ { 3 this(10); 4 } 5 A(int i) { 6 | this(); rr 8H public static void main(String[] args) { 9 | A a = new A(); 10 + } 1 |} Which one is correct? A. B. c. D. E. Only one object is created Multiple objects are created due to recursive call Compilation Error One object is created but throws StackOverflowError exception Multiple objects are created then throws StackOverflowError exception Explanation: If there is any chance of recursive constreutor invocation then we will get compile time error. StackOverflowError occurs if we do not provide the proper terminating condition to our recursive method or template, Which means it will turn into an infinite loop. Some important case studies on recursive constructor calling: Examples of direct recursive constructor calling: Ex- a ex -2 AQ AQ 4 this()5 new AC)5 y y error: recursive constructor invocation] _[Exception in thread “main” [Link]] Examples of indirect recursive constructor calling exe 2 ex-2 AO ¢ AO 4 this(1@); new A(10)5 , i Acint i) { Aint i) { ‘this(); new AC); } ? Terror: recursive constructor invocation] [Exception in thread “ain” java. lang. Stackoverflovérror] x3 a0 ‘this (18); > ACint i) { new AC); > [exception in thread "main" java,[Link]] Week 1 - Day 3 - Question 2 Consider the following coding snippet: 16 17 18 19 20 21 22 23 24 25 Whi A. B. ee, Celass A { 1 AOL [Link]("A class constrcutor"); } } Clelass B extends A { 1 BOL [Link]("B class constrcutor"); } } elass Demo { ] Demo (A a){ System. [Link]("Demo A-arg"); new A()5 } a Demo (B b){ [Link].print1n("Demo B-arg") new B()5 } } Helass Test { ‘1 public static void main(string[] args) { new Demo(null); } } ch one is correct? Compilation Error at line 23 Nul1PointerException at runtime Demo A-arg A class constrcutor Demo B-arg A class constrcutor B class constrcutor + Demo A-arg A class constrcutor B class constrcutor Explanation: The correct option is D, In case of constructor overloading child class argument will get more priority ‘than parent class argument, null is a valid value for any object in java and in the Demo class there are tuo constructors present, and null is a valid argunent for both of then but as here the A class is the parent class and 8 class is the child class so, to at runtime while creating the Deno class object, constreutor having child class 8 type parameterized constructor gets the higher priority than the super class A type parameterized constructor. Now the control flow: new Deno(nul1); -> Deno (B b) constreutor[1st op: Deno B-arg]-> new B(); -> BC) { super()s } constreutor [3rd of class constreutor] -> a(super();) {} constreutor [2nd op: A class constrcutor] Week 1 - Day 4 - Question 1 Consider the following coding snippet: 1 class Multiplication { 2 public int doIt(int x, int y){ 3 return x * y; 4+ } 5} 6 class SpecialMultiplication extends Multiplication { F public int doIt(long x, long y){ 8 return (int)((xty) * (x#y))5 9 } te '} 11 Gelass Test { 12 = public static void main(String[] args){ 13 SpecialMultiplication sm = new SpecialMultiplication(); 14 [Link].print1n([Link](3, 4)); 15 } 16 |} Which one is correct? ae B. 49 C. Compilation Error at Line 14 D. Compilation Error at Line 7 —. Runtime Error ‘The correct option is A. The Specialiultiplication extends Multiplication class means all the fields and nethods available in the Multiplication class is now also available to the SpecialMultiplication class, means doIt(int,int) method is also inherited to this class, but SpecialMultiplication class also has it's on doIt(long, long) nethod. It is not overriding, as the first rule of method overriding is method names and arguments [including order] types must be matched i.e. signature of the method must be same. while calling [Link](3, 4) we are passing 3, 4 which are int type arguments that's why dort(int,int) method inside of SpecialMultiplication class is going to execute. Note: Specialultiplication class has two mehods [dort(int, int) and doTt(1ong,long)] with same name but their parameter declarations are different means here, method overloading is happening. Week 1 - Day 4 - Question 2 Consider the following coding snippet: =n) 1 Dclass Parent { 2 int x; 3. Parent() { 4 addIt(); 5 b 6 = void addtt(){ 7 x #5 55 a; } 9 = void display() { 1e [Link](x) 5 11 Pp oom) 13 Eiclass Child extends Parent { 14 Child() { 15 addIt(); 16 17 5 void addIt() { 18 x += 105 i+ } 20 21 Celass Driver { 22 public static void main(String[] args) { 23 Parent p = new Child(); 24 [Link](); 25 } 26 |} Which one is correct? A. @ B. 15 c. 20 D. Compilation Error at Line 2 E. Compilation Error at Line 24 Explanation: Correct option is C. In case of method overriding method resolution is taken care by JVM based on the runtime object. When the child class object is created, new Child(); in line 23; the Child class constructor executed, and the first Line of every constreutor is super(), that's why the control reaches to Parent class constreutor, in the parent class constreutor the addit(); method is called. As the runtime object is a Child class object that's why Child class addIt(); method is going to execute. Now the x value becomes 10, now the control reaches in the Child class constructor and again the addIt(); method of Child class method is going to execute and the x value will be 20. Week 1 - Day 5 - Question 1 Consider the following coding snippet: 1 Delass Parent { 2 int x = 10; 3 | public void display() { 4 [Link]("Parent(x): " + x)3 5 } a 7 8 LCelass Child extends Parent { 9 int x = 20; 10 [) public void display() { 11 [Link]("Child(x): "+ x)3 oe: | (C} os) aM 14 15 class Test { 16) public static void main(String[] args) { 17 Parent p = new Child(); 18 [Link](); 19 [Link](p.x); 20 } zm} 22 Which one is correct? A. Parent(x): 10 20 [Link](x): 20 10 [Link](x): 1@ 20 [Link](x): 20 10 E. Compilation Error Explanation: In variable hiding chid class variable hides the parent class variable, but the variable resolution is taken care by compiler based on reference type, at line 19 p.x the reference type of this object is Parent type that's why Parent class x variable means 10 will be printed. In method overriding method resolution is taken care by 3VM based fon Runtime object, at line 18 [Link]() the runtine object means the internal object of p is Child type [Parent p new Child()3] that means at runtime child class [Link]() will be invoked, and due to variable hiding parent class x variable is hidden to child class that's why Child(x): 2@ will be printed, so the output will be, Child(x): 2 16 Week 1 - Day 5 - Question 2 Consider the following coding snippet: djiclass a { =] public void m1(){ System. out.print1n("A-m1") 5 } Zi a 4 5 6 “class B extends A { 7 A public void m1(){ 8 [Link].print1ln("B-m1"); 9 eC} 11 Helass € extends B{ 12 (1 public void ma(){ 13 [Link].print1n("C-mi")5; 14 } 16 “class Driver { 17 4 public static void main(String[] args) { 18 A al = new C()3 19 A a2 = new B()3 20 m2(a); 21 m2(a2)5 22 F 23-1 public static void m2(A a) { 24 a.m1()5 25 } 26 |} Which one is correct? A. B-m1 C-m1 B. A-m1 A-m1 C. Compilation Error D. Runtime Error E. None of the above Explanation: The correct option is E. None of the above, at line 20 m2(ai); is called and from m2() at line 14 [Link](); is called, ml() is a Overridden method and in case method overridding method resolution is taken care by VM based on ‘the runtine object and right now, al has C class object as it's internal object that's why C class mi() method is going to execute and the fisrt output is C-m, similarly when m2(a2); is called as the internal object of a2 is of B class ‘type that's why B class mi() method is going to execute and the second output will be B-mi, so the output will be, Week 1 - Day 6 - Question 1 Consider the following coding snippet: Sclass Test { public static void main(String[] args) { 3 int String = 13; char Object = 'A'; 5 [Link]. print (String); 6 [Link] (Object); 8 } Which one is correct? Compilation Error Runtime Error 1A Object References Garbage Values mooop Week 1 - Day 6 - Question 2 Consider the following coding snippet: class Test { public static void main(String[] args) { short x = 10; x= xX £55 5 [Link](x) 5 Which one is correct? A. Compilation Error B. Runtime Error Cc. 50 D. -50 E. None of the above Week 1 - Day 6 - Question 1 Consider the following coding snippet: 1sclass Test { public static void main(string[] args) { 3 int String = 1; a char Object = ‘A's 5 [Link] (String); 6 System. out. print (Object) ; 7 , 8} Which one is correct? A. Compilation Error B. Runtime Error c. 2a [correct] D. Object References E. Garbage Values Explanation: The correct optiuon is C.1A, we can use predefined class name as variable name in java. while evaluating expressions, the intermediate value may exceed the range of operands end hence the expression value will be promoted. Java autonatically pronotes each byte, short, or char operand to int when ‘evaluating an expression. If one operand is a long, float or double the whole expression is promoted to long, float or double [Link] code is attempting to store x + 5 (50), a perfectly valid short value, back into ‘a short [Link], because the operands were autonatically pronoted to int when the expression was evaluated, the result hat also been pronoted to int. That's uhy a lossy conversion fron int to short 4 happening Test { tic void main(stringl} args) { [Link]-print(x); Sacer eens Cos ee eC eeiee ene Ca eer ee: Test { stati main(Stringt] args) € Et short x= 19; ees eee er [r= _Ghort}OT37y . Syaten. out. print (x); Paes Week 1 - Day 6 - Question 3 Consider the following coding snippet: 1 Hclass Test{ 2 public static void main(String[] args){ 2 byte x = 1273 4 X++5 5 Xt+5 6 [Link] (x); 7 } ea} Which one is correct? Ay B. 129 C.. “128 D. 127 E. Compilation Error Week 1 - Day 6 - Question 3 Consider the following coding snippet: 1 fclass Test{ public static void main(stringl] ares){ 3 byte x = 1273 6 [Link](x)5 7 y a) Which one is correct? i 2 129 c. 128, D. 127 E. Compilation Error Explanation: The byte data type is an exanple of primitive data type. It isan 8-bit signed two's conplenent integer. Its value-range lies between -128 to 127 (inclusive). Its minimum value is -128 and maxinum value is 127. Its default value is 0. When we increment xt+ as 127 is the highest value of byte so it causes Byte Overflow. If the result is greater than 127 or less than -128, then the byte variable overflows (1.e., 1t cannot contain the resulting value in a ‘Single byte). But the byte data type in java is cyclic in nature. If x = 127 then xt+ will be -128 and the aj will result -127, so -127 is the answer. Another way to find the answer is, byte: +128 to 127 x= 127 xee is 128 again xt+ is 129 and as 129 is greater than 128(2°7) so we will compare it with 256(2°8) 124 125.,.4128 129....256 297 28 129 - 256 = -127 0 the answer is -127 Week 1 - Day 7 - Question 1 Consider the following coding snippet: 1 Hpublic class Test{ 2c public static void main(String[] args){ 3 int[] x = {35, 36, 037}; 4 for(int i = 0; i < [Link]; i++){ Ss [Link](x[i] + " ")3 6 } 7 + ee} Which one is correct? A. 35 36 37 B. 35 36 31 C. 35 36 037 D. Compilation Error at line 3 E. Runtime Exception at line 5 Week 1 - Day 7 - Question 2 Consider the following coding snippet: a Spublic class Test{ = public static void main(String[] args){ intl] x = {35, 36, €37}5 8 for(int i = 0; i < [Link] it+){ ) aetemotprdatods] #895 y hich one is correct? A. 35 36 37 B. 35 36 31 C. 35 36 037 D. Compilation Error at line 3 E. Runtime Exception at line 5 Explanation: @37 ds an octal number, its equivalent decimal number is 31, that's why the answer is 35 36 31 37 inbase 8 is equal to each cit multipliod wth its conesponding 8% 37, = 3x8147x8" = 2447 = 31 Week 1 - Day 7 -Question 2 Consider the following coding snippet: 1 Elpublic class Test { 2 public static void main(String[] args) { 3 I intl] a = {0,2,4,1,3}3 4 for(int i = @; i < [Link]; i++){ Fy a[i] = a[(a[i] + 3) % [Link]]; 6 } 7 [Link](a[1]); 8 } ey Which one is correct? A.@ Bw c.2 D. Compilation Error E. ArrayIndexOutOfBoundsException Explanation: @1 2 3 4 A=[@2 4 1 3] ize (a[i] + 3) % [Link] = (043) %5 = 3 a[@] = a[3]; = (243) %5 (ali] + 3) % [Link] = (443) % 5 = 2 a2] = a[2]5 (a[i] + 3) % [Link] = (1+3) %5 = 4 a[3] = a[4]; [2 1 4 3 3] 4 4 (ali] + 3) % [Link] = (343) % 5 a4] = a1]; " » Week 2 - Day 1 - Question 1 Consider the following coding snippet: i1sclass Demo { public static void main(String[] args) { for(int i = 03 15 i++) { [Link]("Hello"); break; 8 } Choose the correct option: [Link] Hello Hello.... B. Hello C. Compile time error D. Runtime error Week 2 - Day 1 - Question 1 Consider the following coding snippet: 1sclass Sample { protected int i, j; } ‘class Test { public static void main(String args[]) { Sample s = new Sample(); 3 [Link](s.i + " "+ s.j)3 es } 10 } Choose the correct option: A. Garbage value Garbage Value B. 80 C. Compilation Error at line 8 D. Runtime Error Week 2 - Day 1 - Question 1 Consider the following coding snippe' class Sample { protected int 4, 43 + velass Test ( 6) public static void main(string argst]) { 7 Sample = = new Sanple(); 2 [Link](s.i + ° "+ 5.3)3 a. oo oy Choose the correct option: A. Garbage value Garbage Value B00 C. Compilation Error at line & D. Runtime Error Explanation: In Java, a protected menber is accessible in all classes of the sane package and in inherited classes of other packages with current inherited class reference only. Also, the default constructors initialize integral variables as @ in Java. That is why we get output as 0 @. Week 2 - Day 1 - Question 2 Consider the following coding snippet i -elass Demo { 2° public static void main(string[] args) { 3 for(int i = 0; 13 it+) { 4 ‘[Link]("Hello"); 5 break; 6 y w © a} Choose the correct optior A. Hello Hello Hello. B. Hello €. Compile tine error D. Runtime error Explanation: There is an error in the condition check expression of for loop. Java differs from C++(or C) here. C+ considers all non-zero values as true and @ as false. Unlike C or C++, an integer value expression cannot be placed lihere a boolean is expected in Java. Week 2 - Day 1 - Question 3 Consider the following coding snippet: class Test { static int m1() { static int x= @; return ++x; I public static void main(String args[]) { [Link](m1()); Q } 9 } Choose the correct option: A. @ ob B C. Compilation Error D. Runtime Error Week 2 - Day 1 - Question 4 Consider the following coding snippet: 1selass A { 2 Aint a) { 3 [Link]("Constructor(A class) " + a); 4 y 5} 6 7eclass B { 8 Aa = new A(1); 8s Bint b) { 10 a = new A(b); 11 } 12 public static void main(String[] args) { 13 B b = new B(2); 14 } 15 } Choose the correct option: A. Constructor(A class) 1 Constructor(A class) B. Constructor(A class) Constructor(A class) C. Constructor(A class) Constructor(A class) D. Constructor(A class) Constructor(A class) E. Compilation Error RNENNEB Week 2 - Day 2 - Question 1 lsclass Parent { B protected void m1() { [Link].print1n("Parent-m1"); wn 4 } 5 } 6=class Child extends Parent { 7= void m1() { 8 [Link]("Child-mi"); 9 i 1e@ ii Spublic class Test { 125 public static void main(String args[]) { 13 Child c = new Child(); 14 c.m1(); 15 } 16 } Choose the correct option: A. Parent-m1 B. Child-m1 C. Compilation Error D. Runtime Error Explanation: The order of initialization of local variables comes first then the constructor. (5) The constructor is called in class A and ‘Constructor(A class) 2' is printed. arelass A { 20 A(int a) { (3) The constructor is called in class A and ‘Constructor(A class) 1' is printed. 3 [Link]("Constructor(A class) " + a); 4} 5s} 7eelass B { 8 Aa= new A(1); (2) The instance variable (a), in the class B is allocated to the memory 8 Bint b) { 10 ‘a = new A(b); (4) The constructor of B is called and again a new object of the class A is created. no} 126 public static void main(stringl] args) { 13 Bb = new B(2); (1) b object is instantiated in the main method. “uo (} 15) Week 2 - Day 1 - Question 3 Consider the following coding snippet: Lo elass Test { 2. statie int mi) static int x= 95 return +4x3 public static void main(string args{]) { [Link] .printin(m1()); , 2) Choose the correct option: A. 8 Boa €. Compilation Error D, Runtime Error Explanation: In Java local variables are not allowed to be static. Static variables are variables for whole class(neans class variable). But the scope of a local variable is within the method. So if we make a local variable as static it violates the rule of scope for a static variable as a static variable's scope should not be Linited to a method, that's hy it will give us a compilation error. Week 2 - Day 2 - Question 1 avelass Parent ¢ 24 protected void ma() { 3 [Link]("Parent-mi")3 a} 5 class Child extends Parent { void mi() { System. [Link]("child-mi"); ublic class Test { public static void main(string args[]) { Child ¢ = new Child(); emis Choose the correct option: A. Parent-mi. B. child-mt . Compilation Error D. Runtime Error Explanation: The correct option is Compilation Error. mi() is protected in Parent class and default in Child class. Default access is more restrictive. When a derived class overrides a base class function, more restrictive access can’t be given to the overridden function. If we make m1() public, then the program works fine without any error. Week 2 - Day 2 - Question 2 Consider the following coding snippet: isclass A { 2 int x = 10; 35 public A(int y) { 4 [Link].print1n(x); 5 this.x = y * 10; 6 } zo S5class B extends A { 9 int y = 10; Q5 public B(int y) { 11 super(y); 1 [Link](x); 13 this.x = y * 20; 14 } 15 16"public class Test { 178 public static void main(String[] args) { 18 B b = new B(20); 19 [Link](b.x) 5; 2 } ar ¥ Consider the following coding snippet: A. 10 100 200 B. 10 100 400 Cc. 10 208 400 D. 10 10 10 E. Compilation Error Explanation: aoelass A { 2 int x = 105 35 public ACint y) { (4) from line 11 control will cone here 4 ‘[Link](x); (5) instance variable x has the value 10, so 1st output is 10 5 this.x = y * 19; (6) currently the value of instance varibale x is 1@ and local fu variable y is 28, so the x value will be updated to 200 7 Soelass B extends A { 9 int y = 10; in the constructor the local variable y dominates the instance variable y 36> public Bint y) { (2) from line 18 control will cone here Fes super(y); (3) A class constructor is called with value y = 20 a2 [Link](x)3 (7) from line 5 control cones here, and as 6 class inherits all the properties B this.x sy * 20; of class A, so instance variavle x with value 20 is the 2nd output ou (B) now the value of local variable y is 20, that's why the updated value of x is 400 ‘iepublic class Test { a7= public static void main(string{] args) { 18 Bb = new 8(28); (1) b is instantiated in main() a9 [Link](b.x); (9) curently the value of x is 4e@, that's why 3rd output is 400 2} a} Option Cc. 1¢ 200 400 is the correct option Week 2 - Day 3 - Question 1 Consider the following coding snippet: 1oclass Parent { 2 private void mi() { 3 [Link]("Parent-mi"); 4} 5S) public static void m2() { 6 [Link]("Parent-m2")5 8) public void m3() { 9 m1()5 10 [Link] printin("Parent-m3"); uo} 2 13 } 1a-elass Child extends Parent { as private void mi() { 16 [Link] printin("Child-mi"); wv} 18 public static void m2() { as [Link] println("Child-m2")5 2 (} 215 public void m3() { 22 ma); 23 System. out printn("Child-m3")5 2 (} 25 26 public class Test { 27= public static void main(String args[]) { 28 Parent p = new Child(); 29 p.m2()5 30 p.m3() 5, 31} 32 } Choose the correct option: A. Parent-m2 Parent-mi child-m3, B. Parent-m2 child-mt. Child-m3, €. Child-m2 Parent-m1 child-m3. D. Child-m2 child-ma. child-m3. E. Compilation Error Explanation: io elass Parent ( 2° private void mi() ¢ 3 [Link] println("Parent-ni")s 4) So public static void m2() { 6 [Link] printin(“Parent-n2")s (2) Parent-n2 is the first output # @, 5 public vote ma() ¢ 9 03 » [Link] ,printin("Parent-n3")3 uo} 2s clase Child extends Parent { private void mi() { [Link]("Child-mt"); (5) Child-n1 is 2nd output > public static void m2() { [Link]("Child-n2"); > public void m3() { 10); (4) Parent class private mi() is not visible in Child class, so Child class mi() is invoked [Link]("Child-n3"); (6) Child-n3 is the ard output d ? public class Test { 270 public static void main(String argst]) { 2 Parent p = new Child(); 29 pem2()3(1) for static method overriding is not possible, and due to early binding parent class m2() is called 30 P.m3()3 (3) due to late binding Child class m3() is invoked 31} 32} Correct option is, [Link]-n2 child-ma Child-n3 Week 2 - Day 3 - Question2 Consider the following coding snippet: 1 2 public class Test { 8 Test() { [Link]("Testing..."); } static Test t = new Test(); 85 public static void main(String args[]) { 9 Test t; t = new Test(); 1 } 12 3 Choose the correct option: A. Testing... B. Testing...Testing... C. Compilation Error at line 6 D. Runtime Error Week 2 - Day 3 - Question2 Consider the following coding snippet: public class Test ( Test() { 3 System-out-printin("Testin 2; 6 static Test t = new Tests B+ public static void main(string args[]) { ° Test ts 1 t= new Test()s a} y Choose the correct option: A. Testing. B. Testing. .. Testing... €. Compilation Error at line 6 D. Runtine Error Explanation: statde variables are called while class loading and static variables are called only once, that's why at Line 6, at the tine of class loading Test class constreutor is called once. Then at line 10 a new t class object is instantiated so at that tine also constructor is called, so in total constructor is called twice. That's why B is the errect option. Week 2 - Day 4 - Question 1 Consider the following coding snippet: 1sclass Parent { private int age; 5 public Parent() { age = 45; } 68 public int getAge() { return [Link]; } sclass Child extends Parent { private int age; a public Child() { age = 155 } 2 private int getAge() { return age; } a public static void main(String[] args) { Parent p = new Child(); [Link]([Link]()); } ? Choose the correct option: A. 45 B. 15 C. Compilation Error D. Runtime Error Explanation: While overiding we can't reduce the scope access modifier. [Link]: error: getAge() in Child cannot override getAge() in Parent private int getAge() { attempting to assign weaker access privileges; was public 1 error Week 2 - Day 3 - Question 1 Consider the following coding snippet: class Demo { Demo(int i) { [Link]("int-arg "+ i); } Demo(float f) { [Link]("float-arg " + ); + Demo(double d) { [Link].print1n("double-arg " + d) } Demo(Object 0) { [Link].print1n("object-arg " + 0) } class Test { public static void main(String[] args) { new Demo('a'); new Demo(12.34); new Demo("a"); } Choose the correct option: A. int-arg a float-arg 12.34 object-arg B. int-arg 65 double-arg 12.34 object-arg a C. int-arg 97 float-arg 12.34 object-arg a D. int-arg 97 double-arg 12.34 object-arg a E. Compilation Error Explanation: Correct option is D. int-arg 97 double-arg 12.34 > ED ine py tens —b font —pdoubte object-arg a Rage eae new Demo(")5 char can be converted to int and the Deno(int 4) { [Link]("int-arg " + i); y So the above constructor is going to execute and the ist output is int-arg 97 a" is equivalent ASCII value is 97 new Deno(12.34)5 12.34 is by default considered as double Demo(double d) { [Link]("double-arg * + 4); > $0 the above constructor is going to execute and the 2nd output is double-arg 12.34 new Demo(s"); String class is a child class of object class and in java Parent class reference variable can be use to hold child €lass object, and Child class object is also by default of it's parent type. Deno(object 0) { [Link](“object-arg " + 0)s y So the above constructor is going to execute and the 3rd output is object-arg a Note: Whenever we are trying to print an object internally the toString() of object class will execute to get the string representation of an object. Week 2 - Dav 3 - Question2 Consider the following coding snippet: 1sclass Demo { public static void main(String args[]) { wanvauanw NBO & c } byte x = 645 int 43 byte y; i= ((x << 2) + 2)5 y = (byte) ((x << 2) + 2)5 [Link]("i: "+#i+" y: "+ y)5 isi - 517; y = (byte) (y - 517) [Link].print1n(": Pade y: “a y)s Choose the correct option: AY 130 y: 130 -386 y: -386 130 y: 2 -386 y: -3 258 y: 258 1-259 y: -259 258 y: 2 -259 y: -3 None of these Explanation: byte range in java -128 to 127 z/ 128 = even ---> 2% 128 = Ans z/ 128 = odd = ---> x= 2% 128 x - 128 = Ans x= 64 A= ((x ce 2) + 2) = (64 * 292) + 2 = 258 y = (byte) ((x << 2) + 2) = (byte) 258 = 2 258 / 128 = 2 258 % 128 = 2 4 - 517 = 258 - 517 = -259 (byte) (y - 517) = (byte) (2 - 517) (byte) -515 = -3 515 / 128 515 % 128 = 4 3 ‘The correct option is D. i: 258 Week 2 - day 5 - Question 1 Replace the line 1 and line 2 with the available option, so that we can find the last digit and the first digit of the given number. lselass Test { public static void main(String[] args) { int digit = 12345; int last_digit = // Line 1 int first_digit = // Line 2 é [Link]("first digit " + first_digit); 7 [Link]("last digit "+ last_digit); } ae Choose the correct option: A, digit % 10; % digit / (int)([Link](10, (int)(Math.10g10(12345)))); B. digit % 10; digit / 10; C. digit / 10; digit % 10; D. digit % 10; digit / (int)([Link](10, (int)(Math.10g1@(12345)+1))); f these Explanation: To find the number of digits in a given Integer we use (int)[Link](hath.2ogie(n) + 1) (int )ath.10g10(12345) gives us 4 now (int)[Link](10, 4) gives us 10,000, and if we divide 12,345 / 10,000 then we will get 1.2345, and after converting it to int we will get 1 as our first digit. To find the last digit of a number we use digit % 10 ‘The Correct Option is A Week 2 - day 6 - Question 1 Consider the following coding snippet: :class Demo { public static void main(String args[]) { int i = 3; [Link](++i * 8); [Link](i++ * 8); } Choose the correct option: « 32 32 - 32 40 24 32 - 24 24 - None of these mooowp Explnation: ++i returns the value after it is incremented. it+ return the value before it is incremented. i=3 ++i * 8 = 4 * 8 = 32 [++ has more preference than *] currently i = 4, but incase of ++i * 8, i returns the value 4 then we multiply 4 * 8 means again 32 and then i value is incremented. Option A is correct. Week 2 - day 6 - Question 2 Consider the following coding snippet: loclass Sample { public static void main(String args[]) { double x, y, Z3 int: i; =7% -5; 1.0/0; 0/2.03 = 0/8.03 NS XH [Link](i); [Link].print1n(x); [Link](y); [Link](z); WNPOVMYNAUAWN 15 } Choose the correct option: A. 2 Infinity @.@ Infinity B. -2 Infinity @.@ NaN C. 2 Infinity ©.@ NaN D. -2 Infinity @.@ Infinity E. Compilation Error Explanation: i = 7% -5, the sign of the answer only depends on the sign of the first operand. In java the floating point literals have constant values to represent (10/9.0) infinity either positive or negative and also have NaN (not a number for undefined Like ©/@.0), but for the integral type, we don’t have any constant that’s why we get an arithnetic exception. option c is Correct Week 2 - Day 7 - Question 1 Consider the following coding snippet: 1eclass Demo { public static void main(String args[]) { int x = int y int z= x |= 45 y >= 1; 8 z <<= 1; 9 xX 42 Z5 [Link](x +" "+y +" "4 2); a 2 3 Choose the correct option: A.223 B.2 c.3 D.3 E. N ww AoA lone of these Explanation: e@eeeaeee eeeeeeeR eaeeGeRe eeee2001 ag = = @@@@@2e8 Beeeeeee eaeee020 GA000010 y Z = @@@@8000 BeeeeB0e eaeEGe00 eeee0011 x= 89000800 eGeR0080 GeG20008 20000001 4 eeeeeeee eaeeeeee eeeeeeee e0020100 x | 4 = eeeeeeee eeeeeeee eeeeeeee e0000101 => 5 ye eeeeeeee eeeeeees eedeecee 20000010 y >> 1= — @9ee8e8@ eeeeeeee eeeeeee2 ee000001 or yor =n 2 2) =o z= e8eeee02 eeeeeeee eegRe000 20000011 z<< 1 = 8008080 e2000000 BeeR0000 22000110 or z <<= 1 => (3 * 241) = 6 eeeeeeee eaeeeeee eeeeeeee e0000101 @eeeeeee eaeeeeGe GeeeGeG0 20000110 x * Z = 00080000 e0000000 90000000 20000011 => 3 Option C. 3 1 6 is correct Week 2 - Day 7 - Question 2 Consider the following coding snippet: Belass Check { public static void main(String args[]) { boolean a = true; boolean b = !true; 5 boolean c = a | b; 6 boolean d=a&b; 7 booleane =d?b: c3 8 [Link](d +" " + e); A. true ture B. true false C. false true D. false false E. Compilation Error Week 3 - Day 1 - Question 1 What will be the output of the following Java code? 15eclass Check { public static void main(String args[]) { int a = 5; int b = -~ a3 int ¢; c=a @@@@0@00 eaQe00e0 exEGe000 00000101 +1 eeeeeeee eeeegede BQGRee0e eEREEIIO => 6 As the most significant bit is 1, so the value will be -6 Now ~a is -6, so -~a = -(-6) = 6 so the final answer is 6 Week 3 - Day 1 - Question 2 What will be the output of the following Java code? lsclass Check { public static void main(String args[]) { int a, b= 1; a = 10; if (a != 10 && a / @ == @) [Link](b--); else [Link].print1ln(b++); Choose the correct option: A. ArithmeticException [division by zero in if condition] B. Compilation Error Week 3 - Day 2 - Question 1 What will be the output of the following Java code? &class Check { public static void main(String args[]) { 3 boolean a = false; 4 boolean b = false; [Link](!a * b); 6 [Link](a * b); z } 8 } Choose the correct option: A. true true B. true false C. false false D. false true E. Compilation Error at line 5 Explanation: Logical operator &&(AND), skips evaluating right hand operand if left hand operand is false and this ds called Operator short circuit, thus division by zero in if condition does not give an error. So the else condition is going to execute, now in Post-increment (i++) - After assigning the value to the vardable, the value is incremented. The correct option is D. 2 Explanation: a = false b = false tarb here 2 operators are visible, 1. Logical NOT [Unary Operator, Precednce - 2, Asscociativity - Right to left] 2. Bitwise XOR [Bitwise Operator, Precednce - 9, Asscociativity - Left to Right] Logical NOT has higer precedence than the Bitwise XOR, ta * b= ((!a) “ b) = (true * false) = true a* b= false * false = false Correct option is B. true false Week 3 - Day 2 - Question 2 What will be the output of the following Java code? 1aclass Demo { public static void main(String args[]) { int n1 = 5; int n2 = 6; int n33; n3 = ++ n2 * nl / n2 + n2; System. [Link](n3); } } Choose the correct option: A. 10 B. 11 Cs 32 D. 56 EASTERN RAILWAY owran Division No TrMsc,/752/Post NI For RSLR-SKG new 3“Line Dated: 18.09.2022. SSO/HWH, $5(Gsz) CHC CHC(Chg.) SM(ID &.OD) [Link] (CHG, 1 & 2) SCNLS (all Boards) CW CNL COMM, CM Gece. CPEM(O) TUE ST CML ENGGCNL GUARD ROSTER CTFR OSI HWH AI Ss, Th 8 T1(H)s of HAH Dien [AOM/G, AOM/CHG & AOM/SAP/HMWH i+ AN Branch Officers / Howrah Ove AL ADAMS = HW ‘Ch. 05(6)/HWH for kind Information of ORM. CPIM ECC for kind information. st NI cautions and non avaiblity of Reversible Une For avoiding bunching of trans due tothe Impostion of ‘arrangement hos been made from 19.09.2022 / between Rasulpur ~ Saktgarn, following EMU Local Tra Monday to 25.09.2022 / Sunday: 4. Howrah = Memari= Howrah rain We, | Wowrah Memari | — Train No. Memari (Oep.) (arr). ((ForHowrany | (ep.) 37825 0:05 Thsz—|—basp 07 | 1720 a8 1125 13:07 —[— or spt- 04 ra a9, 1a ie ‘Or Spi: 08 a6 37831 142 03 —| — On spl 08 | —16 3 15:30 ta —} ong v 2. Moweah = Masaoram = Howrah Howrah | Masagram [Train No. | Wasagram {Gen}, “(arr)” _|cror Mourn) | — (Dep) ‘om Bandthaman Maintine - 37834, 37836, 37838, 37840, 37842, 37844 (SAE), 27846 (SAO). [Link] - 36838, 36840, 36842 8 26048, Nate: Other trains will run as usual (0 CARESS (.) ENSILE OFM G 0 aa (arinat A Siva) ‘ot, operations Manager /CHG ‘tor AORM / OP, Howrah Week 3 - Day 3 -Question 1 Which of these lines of Java code will give better performance? teal 4*c>> b&73 2. (al (C4*e) >> b)e&7)) A. 1 will give better performance as it has no parentheses B. 2 will give better performance as it has parentheses C. Both 1 & 2 will give equal performance D. Dependent on the computer system Explanation: n3 = 44 n2 * nd / n2 + n25 In this expression 5 operators are visible, those 5 operators (as per their precendence) are listed below, 1. ++ [highest precedence] 2. sey ale 5. = [lowest precedence] nl = 5 n2= 6 n3 = #4 m2 % nd / m2 + n25 ((((++ m2) * 1) / m2) + n2) (= 5) 1-7) +7) (5/7) +7) G+7) 2 Explanation: Parentheses do not degrade the performance of the program. Adding parentheses to reduce anbiguity does not negatively affect your systen. This shouldn't affect execution time since the expression will be simplified by the compiler. It might have a minute effect on compilation time. Week 3 - Day 3 - Question 2 What will be the output of the following Java code? 1sclass Sample { public static void main(String args[]) { } Choose the A. compile B. runtime Cc. 26:0 20 D. none of int a,b,c,d; at=b-=c*=d/=20; [Link](at" "+#b+" “+c+" "+d); correct option: time error error £ the mentioned Week 3 -Day 4 - Question 1 What will be the output of the following Java code? class Check { public static void main(String args[]) { 3 int a = 1; 4 int b = 6; if ((b = 1) == a) [Link](b++); else [Link](b--); o } } Choose the correct option: A. Here 11 B. Here 1 2 c. Here 21 D. Here 2 0 E. Compilation Error at line 5 Explanation: In this expression all visible operators are Assignment opeartors. This expression will evaluate fron right to left. Week 3 -Day 4 - Question 1 What will be the output of the following Java code? 1eclass Check { 2 E public static void main(String args[]) { 3 int a = 1; int b = 6; if ((b = 1) == a) [Link]("Here 1 " + b++); else [Link]("Here 2 " + b--); 5 fi 10 } Choose the correct option: A. Here 11 B. Here 1 2 C., Here 2 1 D. Here 2 0 E. Compilation Error at line 5 Week 3 -Day 4 - Question 2 What will be the output of the following Java code? leclass Sample { int x: = 53 public static void main(String args[]) { 4 if (x 6&x / @ == @) [Link](“here 1"); else [Link]("“here 2"); Choose the correct option: A. Herel B. Here2 C. Compilation Error D. Runtime Error Week 3 -Day 4 - Question 1 hat will be the output of the following Java code? class Check { Public static void main(string args{]) { int a= 15 int b = 6; if ((b = 1) == a) [Link]("Here 1" + b++)5 else ‘[Link]("Here 2." + b--)5 >} 10} Choose the correct option: A. Here 1.2 B. Here 12 ©. Here 2.1 D. Here 2 0 E. Compilation Error at line 5 Explanation: b ds initialised to 1. The conditional statement returns true and the Here 1 1 will be the output. Week 3 -Day 5 - Question 1 What will be the output of the following Java code? class Check { public static void main(String args[]) { int sum = 0; for (int i = 0, j = 0; [Link](" sum += i; } [Link].print1n(sum) ; } } Choose the correct option: A.5 B.6 Cc. 14 D. Compilation error E. Runtime error Explanatior && and & both don't behave in a similar way expl && exp2 => if expl is false exp2 won't execute anymore, due to short circuit evaluation stops and false is returned. exp & exp2 => both the expression is going to execute and based on the evaluation if any one if false it returs false as the answer. x @ 6&x/e= and when the highlighted part is going to execute it will give runtime exception. Exception in thread "main" [Link]: / by zero at [Link]([Link]) Explanation: i i< e e true 1 2 true 2 3 true 3 4 true 4 5 true <5 i true true true true false Important points to remember: 1. Using comma operator, we can include more than one statement in the initialization and iteration portion of the for loop. 2. && and & both don't behave in a similar way i<5&5<5 sum true e true a. true 5 true 6 false out of loop(sum = 6) expl && exp2 => if exp1 is false exp2 won't execute anymore, due to short circuit evaluation stops and false is returned. expl & exp2 => both the expression is going to execute and based on the evaluation if any one if false it returs false as the answer. Week 3 - Dav 5 - Question 2 What will be the output of the following Java code? 1eclass Demo { public static void main(String args[]) { 3 int x = 2; 4 int y = 6; 5 for (3 y < 10; ++y) { 6E if (y % x == @) 7 continue; BE else if (y == 8) 9 break; 10¢ else [Link](y + " ")5 } + hi Choose the correct option: AJ1357 B.2468 «13579 DBO123456789 Explanation: Whenever y is divisible by x remainder body of loop is skipped by continue statenent, therefore if condition y == 8 is never true as when y is 8, renainder body of loop is skipped by continue statements of first if. Control comes to print statement only in cases when y is odd. Week 3 - Day 6 - Question 1 What will be the output of the following Java coc class Check { public static void main(String args[]) { final int a=10,b=20; 5a while(a> 1) 12 break second; 3 + 14 [Link](a); 15 } 16 [Link].print1n(b); : B 18 } 19 } Choose the correct option: A. 5 10 B.105 cs D. 10 Explanation: Here 3 labelled statements are visible, b >> 1 in if returns 5 which is equal to a, so it will return true, therefore body of if is executed and block second is exited. Control goes to end of the block second executing the last print statenent, printing 10. Week 3 - Day 7 - Question 1 What will be the output of the following Java code? class box { int width; 3 int height; int length; 5 int volume; é void volume(int height, int length, int width) { 7 volume = width*height*length; 8 } 10+class Main { public static void main(String args[]) { box obj = new box(); obj-height = 1; [Link] = 5; [Link] = 55 obj..volume(3,2,1)5 [Link].print1n (obj. volume) ; 18 y 19 } Choose the correct option: Ae Bea c.6 D. 25 Explanation: At Line 13, 14 and 15 we initialized height, length and width respectively but we did not initialized volume, but at line 16 when the volume() method is invoked the non-static variable volume get initialized, and as we have passed 3,2 and 1, so currently the volume value is 6. That's why 6 is the correct option. Week 3 - Day 7 - Question 2 What will be the output of the following Java code? aselass Parent { 2 int x; 3 int y3 4} Saclass Child extends Parent { 6 Child(int x, int y){ Fj this.x = x} 8 this.y = y3 9 y 2 boolean equals(Child c) { i. return this.x == ¢.x && this.y == c.y ? true : false; 2 } 3) 4sclass Test { » public static void main(String args[]) { 6 Parent cl = new Child(5,5)3 7 Child c2 = new Child(5,5); 8 Child c3 = new Child(5,5); 9 [Link]([Link](c2)); 2 [Link]. print1n([Link](c3)); 2 , 22} Choose the correct option: A. true true B. false true C. false false D. true false E. Compilation error at line 19 Week 4 - Day1 - Question 1 Which of the following is not a valid jump statement in java? A. break B. goto C. continue D. return Week 4 - Day1 - Question 2 Which of the following is not a valid flow control statement? A. exit() B. break C. continue D. return Explanation: Java public boolean equals (object anotherObject) method is a method of the Java Object class. This Object class is the root of the class hierarchy in Java i.e. every class in Java has class Object as its superclass. ‘That means every class also has the equals() method in it. In java String class overrides the equals() method. Object class equals() method is responsible for reference comparison means it checks two objects are same or not and String class equals() is responsible for value comparison means it checks two String objects have the sane value or not. Similarly in this program our child class by default has theObject class equals() method but we have overloaded the equals() method (at line 19) for value calculation. So at line 19 when [Link](c2) is called then as the Parent class has only the Object class equals method so it will perform reference calculation and as cl and ¢2 are complete different objects, it will return false. But at line 20 [Link](c3) is invoked then the Child class boolean equals(Child c) method is going to execute and it will perform value comparison returns true. Week 4 - Day - Question 1 Which of the following is not 2 valid jump statement in java? A. break B. goto €. continue D. return Explanation: The Coorect option is b, as break, continue and return transfer control to another part of the program and returns back to caller after execution. Houever, goto is marked as not used in Javs Week 4 - Day1 - Question 2 lihich of the following is not a valid flow control statement? A. exit() B. break C. continue D. return Explanation: exit() is not a flow control statement in Java. exit() terminates the currently running 3VM. Week 4 - Day 1 - Question 3 What will be the output of the following Java program? class Demo { public int x; 3 static int y3 void cal(int a, int b) { 5 X45 a5 y += bs } } class Test { a= public static void main(String args[]) { 1 Demo obj1 = new Demo(); Demo obj2 = new Demo(); obj1.x = 93 1 obji.y = @ [Link](1, 2); obj2.x = 2} [Link](2, 3); [Link](obji.x +" " + obj2.y); } } Choose the correct option: A. 12 B. 23 & 22 D415 E. None of the above Explanation: at Line number 15, when [Link](1, 2) executes currently instance x has 1 and static variable y has 2, before line 17 executes, currently ob2.x is @ and obj2.y is 2, when line 17 [Link](2, 3) executes, the instance variable of obj2.x currently initilized with 2 but obj2.y previously had the value 2 and now it is initialized with 5 (y += bj where b is 3) that means obji.y is also initialized with 5. So the right option is 1 5. Week 4 - Day 2 - Question 1 What will be the output of the following Java program? icelass Check { 2 static int x; void increment() { X+45 } + class Test { public static void main(String args[]) { 9 Check obj1 = new Check(); 1e Check obj2 = new Check(); 1 obj1.x = @; [Link](); [Link](); 1 [Link].print1n(obj1.x +" " + obj2.x); + } Choose the correct option: A: ay? B11 C22 D. Compilation Error —. Runtime Error

You might also like