aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes More basic concepts 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 "More basic concepts" Watch "More basic concepts" New topic
Author

More basic concepts

mo odunaiya
Greenhorn

Joined: Jul 23, 2001
Posts: 19
Can anybody help?
Quick, concise, comments will do !!
Question ID :957736837310
Consider the following class :
class Test
{
public static void main(String[] args)
{
if (args[0].equals("open"))
if (args[1].equals("someone"))
System.out.println("Hello!");
else System.out.println("Go away "+ args[1]);
}
}
Which of the following statements are true if the above program is run with the command line :
java Test closed
*****************************************************************
Ans: It won't print anything
why wont this programe print anything? I honestly thought the first arguement will be outputed
*****************************************************************
Question ID :952739442300
What will be the result of attempting to compile and run the following program?
class TestClass
{
public static void main(String args[])
{
boolean b = false;
int i = 1;
do
{
i++ ;
} while (b = !b);
System.out.println( i );
}
}

*****************************************************************
ans 3,
Question: how will the loop body be executed twice? while the do loop is true, shouldn't i++ be incremented by 1 to give 2, why is the answer 3?
*****************************************************************
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
For your first question the else corresponds to the second if statement. Since first condition is not met(closed is supplied in place of open) hence no if loop is entered.
For your second question
Here is the sequence
i = 1
enter do loop
i = 2
execute while(b=!b)
no b=!b will result in true because b is false hence !b is true
enter do loop again
i = 3
execute while(b=!b)
this time b=!b results in false hence no more execution of do...while take place.
[This message has been edited by Anshul Manisha (edited July 24, 2001).]


AM<BR> <A HREF="mailto:anshulmohan@rediffmail.com" rel="nofollow">anshulmohan@rediffmail.com</A>
Namrata Shetty
Ranch Hand

Joined: Jul 18, 2001
Posts: 30
1.
class Test
{
public static void main(String[] args)
{
if (args[0].equals("open")) //1st if statement
if (args[1].equals("someone")) //2nd if statement
System.out.println("Hello!");
else System.out.println("Go away "+ args[1]);
}
}
Which of the following statements are true if the above program is run with the command line :
java Test closed
The 'else' statement corresponds to the second 'if' statement.
since the first 'if' statement evaluates to false it exits out of the method and there is no output.
in fact if the program ever executed the statement
System.out.println(args[1]); in the above case it would give a runtime error since the first argument passed at command is args[0]
2.
class TestClass
{
public static void main(String args[])
{
boolean b = false;
int i = 1;
do
{
i++ ;
} while (b = !b);
System.out.println( i );
}
}
1st time the do loop is entered into i=1 b=false
when it exits the do loop i=2; b=true;
since the while(condition) is while(true) the do loop is entered the 2nd time
2nd time do loop is entered i=2; b=true;
when it exits the do loop i=3; b=false
now the while(condition) is while(false) so it executes the next statement which is the print statement
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi,
To sum up, if you have
<pre>
if (condition)
.....;
if (condition)
.....;
else
.....;
</pre>

The else would pair up with the immediate inner if loop
For the do..while loop,
<pre>
do {
.....
}while(condition)
</pre >

The statement would executed before the condition is checked, and then depending on the whether the condition is true, it would be executed as many times as possible.
Hope this helps,
Sandeep
SCJP2, OCSD(JDeveloper), OCED(Oracle Internet Platform)


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
mo odunaiya
Greenhorn

Joined: Jul 23, 2001
Posts: 19
Thank you all,
......the concept is clear !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: More basic concepts