• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

voodoo exam_1 question

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
The Java Language Specification does indeed state that the main method must be declared public, 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
  • 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
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic