aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion" Watch "Assertion" New topic
Author

Assertion

uzma Akbar
Ranch Hand

Joined: Sep 21, 2005
Posts: 40
class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: throw new AssertionError();
}}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}

Which statements are true?

a. With assertions enabled it prints ABC followed by an AssertionError message.
b. With assertions disabled it prints ABC followed by an AssertionError message.
c. Assertions should not be used within the default case of a switch statement.
d. In this code example an assert statement could not be used in place of the "throw" statement

Correct Answers are : a,b,d
I could not understand how d is correct. please help. Thanks in advance
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: throw new AssertionError();
}}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}

In Above Example Return Type Of Method m1() is String.i.e. It Has To Return Either String Or Throw An Exception.If There Have Been An Assert Statement ,method m1() Would Not Have Returned Any String Or Thrown Error Condition Wen Asserrt Is Enabled That's Why It Wud Have Caused Error.
uzma Akbar
Ranch Hand

Joined: Sep 21, 2005
Posts: 40
class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default:
assert false;
}
}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}
Which statements are true?

a. With assertions enabled it prints ABC followed by an AssertionError message.
b. With assertions disabled it prints ABC followed by an AssertionError message.
c. Assertions should not be used within the default case of a switch statement.
d. In this code example a throw statement must be used in place of the assert statement.
e. Compile-time error


Answer : d,e


class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default:
assert false;
}
return "E";
}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}

Which statements are true?

a. With assertions enabled it prints ABC followed by an AssertionError message.
b. With assertions enabled it prints ABCE followed by an AssertionError message.
c. With assertions disabled it prints ABC
d. With assertions disabled it prints ABCE
e. Assertions should not be used within the default case of a switch statement
Answers: a,d


My question is why not in first case compiler error is thrown. Your help is appreciated. Thanks
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default:
assert false;
}
}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}
Which statements are true?

a. With assertions enabled it prints ABC followed by an AssertionError message.
b. With assertions disabled it prints ABC followed by an AssertionError message.
c. Assertions should not be used within the default case of a switch statement.
d. In this code example a throw statement must be used in place of the assert statement.
e. Compile-time error


Answer : d,e

*********************************************************************8
As You Know assertions Are Not Activated by default.so if assertion is disabled,what will happen wen i doesn't match any of the case and reaches to default statement?--it will not return anything right?but Its return type is String(so it can either return Strind or throw ab error condition) so due to compatibility problem compile time error will be there........right?So It Must be Relplaced by throw AssertionError which is always enabled
now Think In This Very Manner for your 2nd problem
Right?
##########################################
Agrah
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Please Use Code tags. That makes your code easily readable.
These tags are at the bottom of the page.

Thanks


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Originally posted by agrah upadhyay:
class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default:
assert false;
}
}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}
Which statements are true?

a. With assertions enabled it prints ABC followed by an AssertionError message.
b. With assertions disabled it prints ABC followed by an AssertionError message.
c. Assertions should not be used within the default case of a switch statement.
d. In this code example a throw statement must be used in place of the assert statement.
e. Compile-time error


Answer : d,e

*********************************************************************8
As You Know assertions Are Not Activated by default.so if assertion is disabled,what will happen wen i doesn't match any of the case and reaches to default statement?--it will not return anything right?but Its return type is String(so it can either return Strind or throw ab error condition) so due to compatibility problem compile time error will be there........right?So It Must be Relplaced by throw AssertionError which is always enabled
now Think In This Very Manner for your 2nd problem
Right?
##########################################
Agrah


Agrah what type of compatibility problem are you talking about?
[ September 26, 2005: Message edited by: Sandeep Chhabra ]
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
Expectation of compiler to get returned a specific return-type.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertion