Help coderanch get a
new server
by contributing to the fundraiser
  • 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

John Hunt mock exam

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think question 62 in this exam has a wrong answer.the correct choice should be B not A
the code was something like:
public class outer{ //1
class inner{ //2
void test(){ //3
if(outer.this.flag){ //4
sample(); //5
} //6
} //7
private boolean flag=false; //8
void sample(){ //9
System.out.println("sample"); //10
} //11
public outer(){ //12
new inner().test(); //13
} //14
public static void main(String args[]){ //15
new outer(); //16
} //17
} //18

the code will compile but no o/p will be generated.according to the anser given in mock exam code will print sample.
if i define boolean variable in main().i get the following compilation error
1.line 15 should have }
2.for the boolean variable(defined in main) statement expected
3.line 16 type expected

please help?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have the code wrong. In the actual question it is:
class Inner {
void test() {
if (Test4.this.flag); {//NOTICE THE ;
sample();
}
}
}
Notice the semicolon immediately after if(condition). That is an empty statement and does nothing. The block {} after the semicolon is executed whether the condition for "if" is true or false. Therefore the given answer is correct.
However, if you compile and run the code as given in your post, it will produce no output.
[This message has been edited by Vagisha Sharma (edited March 22, 2001).]
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Vagisha
but why in my code declaring boolean in main() giving so many errors?
help!
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It SOUNDS like it is complaining about a typo, but since we can't see how you typed it . . .
 
Vagisha Sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy is right, it does sound like a typo. Why don't you post your code here?
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup!
one mistake in typo..but look at the following (same type of code)but i am not getting the o/p.please help!

public class test1{
class inner{
void test(){
if(test1.flag);
sample():
}
}
void sample(){
System.out.println("sample");
}
public test1(){
new test1();
new inner().test();
}
public static void main(String args()){
static private boolean flag=false;
}
}
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You can't declare a static member variable inside a method.
This code will work fine.
//
//
public class Mar24 {
class inner{
void test(){
if(Mar24.flag);
sample();
}
}
void sample(){
System.out.println("sample");
}
public Mar24(){
new inner().test();
}
static private boolean flag = false;
public static void main(String args[]){
new Mar24();
}
}

[This message has been edited by Nasser Aboobaker (edited March 24, 2001).]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Nachiket,
Firstly your code has been typed incorrectly --->
1.main method hasn't got a string array.
2.method sample hasnt got a semicolon.
3.you have used a static boolean variable inside main and is out of scope.
4.u have a dynamic object created inside the constructor.
5.My possible solution to the code is --->
public class test1{
static private boolean flag=false;
class inner{
void test(){
if(test1.flag);
sample();
}
}
void sample(){
System.out.println("sample");
}
public test1(){
new inner().test();
}
public static void main(String args[]){
new test1();
}
}

pls correct if i'm wrong.
Karthik
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the typo Karthik!
in my original programme there are no typos, the programe is not giving any ERROR AT ALL.it is not complaning for declaring static variable inside main()!
it's not giving any RUNTIMError as well.only thing is no output which should be "sample"according to System.out.println().
need help.
 
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 nachiket,
Your code sample:

causes the following compile-errors:

When executed using JDK 1.3. Are you using an IDE to compile? Assuming you are, and the IDE is producing a clean compile, the reason you are not seeing any output is because you are not instantiating an instance of the class.
Comment out <code>new test1()</code> in your constructor (you'll get a bunch of runtime errors if you leave it in) and add <code>new test1()</code> in <code>main()</code>
Hope that helps.
PS If you are studying for the SCJP2 it's really much more useful to compile examples from the command-line using the JDK vs using an IDE. Most IDE's do optimizations and various other things to your code. The exam questions are based on you knowing what will happen using the JDK.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic