Question : What is the behavior of the below code?
package com.hadoopexam;
class Welcome { public Welcome() { System.out.println("In Welcome"); }
public class Company { public Company() { System.out.println("In Company"); } } }
class TestColor { public static void main(String[] args) { Welcome.Company comp = new Welcome().Company(); // n1 } } 1. It will compile without any error but give RuntimeError
2. It will also run without any error and print "In Company"
3. It will also run without any error and print "In Welcome"
4. It will not compile as it will give error at line n1.
5. If you want to create an instance of an inner class. Then you also have to create instance of an outer class. As below Welcome. Company comp = new Welcome().new Company();
Correct Answer : 4 Explanation:
Question : You have been given below code, however you see there is some compilation error. How would you resolve this.
void doStuff() throws ArithmeticException, NumberFormatException, Exception { if (Math.random() > -1) throw new Exception("There is an Error , using this code. Contact www.HadoopExam.com"); }
}
1. We should remove following code segment catch (Exception e) { System.out.println(e.getMessage()); }
2. Replace this line catch (ArithmeticException | NumberFormatException | Exception e) With catch (Exception | ArithmeticException | NumberFormatException e)
3. Replace this line catch (ArithmeticException | NumberFormatException | Exception e) With catch (ArithmeticException | NumberFormatException e)
4. There will be no compilation error.
Correct Answer : 3 Explanation: In a multi-catch block, you cannot combine catch handlers for two exceptions that share a base- and derived-class relationship. Here, Exception is a base class for all the Exceptions. You can only combine catch handlers for exceptions that do not share the parent-child relationship between them. Hence, to remove compilation error we should have removed Exception e
Question : You have been given below code, what would be the behavior of the given code? Both the classes are in their respective files.
package com.hadoopexam;
class Welcome { private boolean isCompany; protected int totalProductss;
public Welcome() { isCompany = false; totalProductss = 0; }
public class Company { public void print() { System.out.println("isCompany: " + isCompany); System.out.println("totalProductss: " + totalProductss); } } }
package com.hadoopexam;
class Test { public static void main(String[] args) { Welcome.Company comp = new Welcome().new Company(); comp.print(); } }
1. It will throw compile time error.
2. It will compile but throw RuntimeError
3. It will run and compile successfully and print below isCompany: false totalProductss: 0
4. Compiler error: an inner class cannot access private members of the outer class
Correct Answer : 3 Explanation: There is no restriction that inner class can not access private member of outer class. Inner classes can access any member variable from outer class without considering outer class access modifiers.