Sunday 7 April 2013

MCQ #13

Question) Given this code what is the output?

class Exam
{
    protected String level = "Easy";

    public void printLevel()
     {
         System.out.println(level);
      }


Class IITExam extends Exam
{
     private String level = "Difficult";


 IITExam  iitExam = new IITExam();
 iitExam .printLevel();
 
Options)


  • Difficult
  • Easy
Answer) Easy

Explanation) Methods can be overridden not the attributes. Rest is self explanatory.

MCQ #12

Question) Given this code what is the output?

boolean b = false;
if(b = true)
{
   System.out.println("Yes");
}

Options)

  • Yes
  • Nothing is printed
Answer) Yes

Explanation) If you are puzzled looking at the output observe the code again. Inside the if condition you have an assignment operator(=) and not a equality operator(==) . Assignment will always be true regardless of anything and hence the code inside the for loop will always execute. Hence the output will be "Yes". Also note that b will be set to "True".
t> UA-39527780-1 back to top