• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jtips mock exam

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. Given:
public class Q1 {
static void processorB() {
System.out.println(" is a boolean Operator.");
}

static Q1 processorA(){
Q1 q=null;
if (("java").startsWith("null"))
return q;
else
return null;
}

static {
System.out.print("Java".startsWith(""));
}

public static void main(String[] args) {
processorA().processorB();
}
What is the result?
a) Prints false is a boolean Operator.
b) Line 8 throws NullPointerException at Runtime.
c) Prints true is a boolean Operator.
d) Compile-time error at line 8.
e) None of the above.
correct answer is c
can someone explain how
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What a bizarre piece of code.
The static blocks will execute first. The code fragment simply prints "True" (since the result of any String.startsWith("") will always return True).
The processorA() method returns an instance of the Q1.class, which has no output or meaning when the class is loaded. However, when the same method is called in main(), the instance is used to call the processorB() method, which prints the remainder of the output.
Lord help me if I ever run across code like this in real life.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sona,
Here goes:
The first thing to happen is that the class Q1 gets loaded and all static blocks are executed. Your code include the static block:

At this point it might help to see the code inside startsWith():

We can see that the while loop will never execute because the string length is zero. Therefore the startsWith will return true. So for the first print we get true.
The next thing to happen is that your main method gets executed. Inside your main method you are calling processorA() method. Regardless of the if statement result we will be returning a null pointer. Now here is where it gets interesting. The method processorB() is defined as static! What that means is that the compiler doesn't need an object reference to get to it. If the compiler did need an object reference it would choke on the null pointer, but since it doesn't need it the execution continues.
The method processorB() prints out the string is a boolean Operator.".
Therefore the final result after all that is the string:
true is a boolean Operator.
Regards,
Manfred.
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, Manfred is correct; I didn't note that processorB() was static. So the whole processorA() method is a red herring.
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks manfred
its clear as water now
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic