• 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

Mock Exams

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am currently studying for the Java 2 exam,....second time around, i am using the jqplus software and I find it does cover many areas in the Sun's syllabus which I never touched when I took my first exam.
Can anybody help me with these questions?
I will be grateful if your analysis/logic/reasoning behind these questions can be simplified in analogy and concise.
Question ID :970937185650
FlowControl
public class BreakTest
{
public static void main(String[] args)
{
int i = 0, j = 5;
lab1 : for( ; ; i++)
{
for( ; ; --j) if( i >j ) break lab1;
}
System.out.println(" i ="+i+" , j = "+j);
}
}
What will it print?
********************************
i= 0, j = -1
********************************
Flow Control
class Test
{
public static void main(String[] args)
{
for (int i = 0; i < 10; i++) System.out.print(i + " "); //1<br /> for (int i = 10; i > 0; i--) System.out.print(i + " "); //2
int i = 20; //3
System.out.print(i + " "); //4
}
}
Which of the following statements are true?
***********************************************************************
class will compile and print 20
it will not compile if line 3 is removed
it will not compile if line 3 is removed and placed before line 1
it will not compile if line 4 is removed and placed befor line 3
option 2,3, and 4 are correct
************************************************************************
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi muhammedo,
w.r.to ur second question the answer is "class will compile and print 20" because,method level variable declaration will be initialized from top to bottom fashion, that is
please see the following two codes
***Code 1 ***
public static void main(String ar[]){
int x = 100;
for(int x=0; x>99; x++); // Compile Error
}
*** Code 2 ***
public static void main(String ar[]){
for(int x=0; x>99; x++);
int x=100;
System.out.println(x); // prints 100
}
In the code 2 the reference for x will be created and destroyed in for loop itself(block level),so int x=100 in code 2 will create a new reference for x again.
But in code 1 the reference for x is already defined and u can't create a new one in for loop.
correct me if i'm wrong..
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Balaji,
I think you are missing something here, see the first option says that it will compile and print 20. This program will print not only 20 but also other SOP statements. so the first option is not correct.


class Test
{
public static void main(String[] args)
{
for (int i = 0; i < 10; i++) System.out.print(i + " "); //1<br /> for (int i = 10; i > 0; i--) System.out.print(i + " "); //2
int i = 20;
//3 if this line is removed then compiler error- i not intialized
System.out.print(i + " "); //4
}
}
Which of the following statements are true?
class will compile and print 20
it will not compile if line 3 is removed
it will not compile if line 3 is removed and placed before line 1
it will not compile if line 4 is removed and placed befor line 3
option 2,3, and 4 are correct


Thanks,
Vanitha.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
For the first question, see the comments please.


public class BreakTest
{
public static void main(String[] args)
{
int i = 0, j = 5;
lab1 : for( ; ; i++) //i = 0
{
for( ; ; --j) if( i >j ) break lab1;
/*in this for loop there is no condition to check, so when i =0
j is decremented, when the if condition is checked, that is i = 0 and j = -1 it will break the lab1 outer for loop.
*/
}
System.out.println(" i ="+i+" , j = "+j);
}
}
What will it print?
********************************
i= 0, j = -1
********************************


Hope this helps,
Vanitha.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Scope of Variable declared in for loop is limited to its very own block, i.e the variable is no more at the end of the for block. In order to print i on line 4, variable i has to be defined before it is used in the print statement. If you want to define variable i in the first line, you will get compile error because then you can't declare the same variable in the for loop.
--Farooq
 
Balaji Loganathan
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vanitha Sugumaran:
Hi Balaji,
I think you are missing something here, see the first option says that it will compile and print 20. This program will print not only 20 but also other SOP statements. so the first option is not correct.
Thanks,
Vanitha.


you are right and thanks for reply's
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi muhammedo
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic