• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

voodoo exam_1 question

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By changing which line in the following code can you make the class compile with no errors?
class Xtc {
protected static void main(String args[]) {//line 1
byte b = 10, //line2
c = 10 , //line 3
x= (byte) (b + c) ; //line 4
System.out.println(" x is " + x); //line5
}
}
Options
a) line 1
b) line 2
c) line 3
d) line 4
e) line 5
f) None , the class compiles with no error and prints 20
He says the ans is (f) None
I marked it as (a) since main() must be public as the class will compile only if line1 is changed to public static void main ( String args[])
can anyone tell me why the answer is none, i tried compiling the program but compiler says main must be public and static.
thanks in advance.
bani
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer should to be error in the line of main method declaration. since main() has to be declared public and static.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java Language Specification does indeed state that the main method must be declared coderanch, static, void and have a single argument that is an array of Strings. However, it is usually the JVM that enforces the rule. For that reason, a Sun compiler will compile the code as long as it is a legal method declaration, but the JVM will throw a runtime error if an attempt is made to invoke the main method from the command line.
Older versions of the JDK do not enforce some of the rules, so it is possible to get a variety of results if you attempt to compile and run the code. However, for the purposes of the exam, the rules specified in the Java Language Specification must be enforced.
 
bani kaali
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Zarina.
Don,thanks for the explanation.
Your exams are of great help to people like me who are preparing for SCJP.You have done a great job.Thanks again Don.
-bani
 
reply
    Bookmark Topic Watch Topic
  • New Topic