Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

Questions from JavaCaps Mock Exam 1

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please explain why answer is A and not D ??
10. What is the result when you compile the and run the following code?
public class ThrowsDemo {
static void throwMethod() {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwMethod();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
A) Compilation error
B) Runtime error
C) Compile successfully, nothing is printed.
D) Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo
Answer 10:
A) Compilation error

-------------
Can you please explain why answer is C and not A or B?

20. What is the output of the following code?
public class TestLocal {
public static void main(String args[]) {
String s[] = new String[6];
System.out.print(s[6]);
}
}
A) A null is printed
B) Compile time error
C) Exception is thrown
D) null followed by 0 is printed on the screen
Answer 20: C) Exception is thrown
thanx
[ September 17, 2002: Message edited by: Richa Jeetah ]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
For Q:20
Because Array is length of 6. i.e. [0] to [5]..
When it's going to print [6] th element, it throws ArrayIndexOutOfBoundsException..
[ September 17, 2002: Message edited by: skraval ]
[ September 17, 2002: Message edited by: skraval ]
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Run it and you'll see why. You're going off the end of the array.
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
With s[6] The value of the index is equal to s.lenght and thus you're trying to access a value ouside the bounds (min=0, max=5) and that's why an exception is throw and more prcisely ArrayIndexOutOfBoundsException
 
Richa Jeetah
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by skraval:
Hi,
For Q:20
Because Array is length of 6. i.e. [0] to [5]..
When it's going to print [6] th element, it throws NullPointerException..
[ September 17, 2002: Message edited by: skraval ]



And if it were [5] then correct answer would have been A ??


[ September 17, 2002: Message edited by: Richa Jeetah ]
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Q10:
IllegalAccessException is a checked exception, but you have not declared it in a throws clause.
For Q20: I strongly recommend that you compile and run this code, then edit it and compile and run it again. That's the best way to understand what it does.
[ September 17, 2002: Message edited by: Ron Newman ]
 
Richa Jeetah
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i ran the code for Q20 and Q10, now its clear to me.
Thank You
 
smita raval
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Richa,
"And if it were [5] then correct answer would have been A ??"
Yes, of course.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic