• 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

Confused about correct answers in the mock exam

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are the questions the answers to which baffled me based on what I learnt from other books plus the wording of the answers.
This from the JExam mock exam.
Here are the questions. (Ignore HTML Tags)
What is the output from the following piece of code:
loop1:

for(int i = 0; i < 3; i++){

loop2:

for(int j = 0; j < 3; j++){

if (i == j){

break loop2;

}

System.out.print("i = " + i + " j = " + j + " ");

}

}
There are several answers :-
1. Some number output
2. Some number output
3. Some number output
4. None of the above.
According to Bill Brogdens book, Java Cram, he says that a label cannot stand by itself, it has to be part of a statement, This turns out to be wrong. I successfully compiled and ran the above example.
Any comments ?
Q.2
Before which of the following can the keyword "synchronized" be placed, without causing a compile error.
Choices:
1. class methods
2. instance methods
3. variables
4. classes
My Choice ... 1,2,3 .... but the exam says choice 3 is not correct
I compiled and ran it works. Although choice 3 is not a sensible thing, but question says will it compile or not... it does.
Any comments.
Q.3. I dont have the exact one, but this is how it goes.
How many components can you add to a sector of a BorderLayout Container
Answer:
1.One
2.Any number of Components
3.Some other choice
The answer was: You can add only one Component to any sector, if you want to add more than one, then add the components to another container such as Panel and then add the panel to the sector.
This is the correct explaination, but the question is not how many components are displayed in a sector, the question is how many you can add. I can add as many as I want ( cant I) but only one will be displayed. example the CENTER.
I can go on adding, only the last one will be visible, but complier allows me to add.
Please advise, if I am wrong in reading the question correctly or its close to Friday and everthing is not fair in love and war.
Thanks in advance.

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q1: The label is part of the "for" statement.
Q2: I tried compiling
class TestSynchronized1{
synchronized int i = 0 ;
}
class TestSynchronized2{
void aMethod() {
synchronized int i = 0 ;
}
}
Both produced compilation errors.
Q3: I agree with you. I thought the same thing when I did that question.
 
Ramnath Krishnan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Q2
try this
public class Test{
//inside main...
int i = 0;
synchronized(i){
System.out.println(i);
}
}
It works..
 
Ramnath Krishnan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Terribly sorry.
Q2 does not compile and the previous example does not compile
The answer is correct, cannot put synchronized in front of a variable
only synchronize on an object.
so saying
String tmp = "Duh!!!" ;
synchronized(tmp){} is correct as tmp is a String object.
Sorry for the misunderstanding.
 
David Wake
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmm. I tried it and got a compile-time error: "The type of this expression, "int", is not a valid reference type in this context".
But it you had used Integer instead of int, it would have compiled correctly, so your answer is correct.
I think the question should have been: In which of the following *declarations* can "synchronized" be used.
 
David Wake
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget that variables can refer to objects as well as primitives. So I actually think your original answer was correct, as the original question was worded.
 
Ramnath Krishnan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct, variable can hold object references, but dont they have to be resolved at compile time, so when you declare a variable, the type of that variable should be at least an Object for the synchronized to work with it.
So int i = 0 with synchronized might not work, but
Object i = MyObject
synchronized(i){} will definitely work
Great .... Thanks;
reply
    Bookmark Topic Watch Topic
  • New Topic