• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

SCJP exam Query....Assertions....

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Next week I am going to write SCJP exam.I saw some mock exam in assertions , it shows the following questions. It looks like out of syllabus(objectives).In K&B book assertions topic I didn't see any word like "invariant" or "precondition".Should I go that much deep in assertions?Please anyboby confirm me.Is it enough to study K&B book?.Advance Thanks.
The questions are,
class D {
private boolean b1, b2;
public void setB1(boolean b) {b1 = b;}
public void setB2(boolean b) {b2 = b;}
public void m1 () {
if (!b2 & !b1) {System.out.print("A");
} else if (!b2 & b1) {System.out.print("B");
} else if (b2 & !b1) {System.out.print("C");
} else {assert false;}
}
public static void main (String[] args) {
D d = new D();
d.setB1(true); d.setB2(true);
d.m1();
}}

Which statements are true?

a. With assertions enabled it prints an AssertionError message.
b. With assertions enabled it prints nothing.
c. With assertions disabled it prints an AssertionError message.
d. With assertions disabled it prints nothing.
e. An assertion should not be placed at any location that the programmer believes will never be reached under normal operating conditions.
f. The assert statement is being used to check a control-flow invariant to verify that the control flow never reaches a point in the program.

--------------------------------------------------------------------------------

Question 15
class A {
private void m1 (int i) {
assert i < 10 : i; System.out.print(i);
}
public void m2 (int i) {
assert i < 10 : i; System.out.print(i);
}
public static void main (String[] args) {
A a = new A(); a.m1(11); a.m2(12);
}}

Which statements are true?

a. If assertions are enabled at run time it prints an error message.
b. With assertions enabled it prints nothing.
c. With assertions disabled it prints an error message.
d. With assertions disabled it prints 1112.
e. With assertions disabled it prints nothing.
f. The assert statements are being used to check a precondition--something that must be true when the method is invoked.
g. Method m1 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a non-public method.
h. Method m2 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a public method.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest you to stick to K&B. I found the tone and style of questions
asked in real exam much similar to K&B.


Thanks,
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a short guide which would help you out. I would say that you should get familiar with these terms.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 1 -->
class D {
private boolean b1, b2;
public void setB1(boolean b) {b1 = b;}
public void setB2(boolean b) {b2 = b;}
public void m1 () {
if (!b2 & !b1) {System.out.print("A");
} else if (!b2 & b1) {System.out.print("B");
} else if (b2 & !b1) {System.out.print("C");
} else {assert false;}
}
public static void main (String[] args) {
D d = new D();
d.setB1(true); d.setB2(true);
d.m1();
}}

Which statements are true?

a. With assertions enabled it prints an AssertionError message.
b. With assertions enabled it prints nothing.
c. With assertions disabled it prints an AssertionError message.
d. With assertions disabled it prints nothing.
e. An assertion should not be placed at any location that the programmer believes will never be reached under normal operating conditions.
f. The assert statement is being used to check a control-flow invariant to verify that the control flow never reaches a point in the program.

--------------------------------------------------------------------------------

Answer to this question is a,d,f

Question 15
class A {
private void m1 (int i) {
assert i < 10 : i; System.out.print(i);
}
public void m2 (int i) {
assert i < 10 : i; System.out.print(i);
}
public static void main (String[] args) {
A a = new A(); a.m1(11); a.m2(12);
}}

Which statements are true?

a. If assertions are enabled at run time it prints an error message.
b. With assertions enabled it prints nothing.
c. With assertions disabled it prints an error message.
d. With assertions disabled it prints 1112.
e. With assertions disabled it prints nothing.
f. The assert statements are being used to check a precondition--something that must be true when the method is invoked.
g. Method m1 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a non-public method.
h. Method m2 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a public method.

answer to this question is a,d,h

Please corrent my answers in I am wrong.

Thanks in advance.

regards,
Satabdi
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by satabdi das:

Question 15
class A {
private void m1 (int i) {
assert i < 10 : i; System.out.print(i);
}
public void m2 (int i) {
assert i < 10 : i; System.out.print(i);
}
public static void main (String[] args) {
A a = new A(); a.m1(11); a.m2(12);
}}

Which statements are true?

a. If assertions are enabled at run time it prints an error message.
b. With assertions enabled it prints nothing.
c. With assertions disabled it prints an error message.
d. With assertions disabled it prints 1112.
e. With assertions disabled it prints nothing.
f. The assert statements are being used to check a precondition--something that must be true when the method is invoked.
g. Method m1 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a non-public method.
h. Method m2 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a public method.

answer to this question is a,d,h

f is correct too.
 
It's exactly the same and completely different as this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic