• 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

Codes result and output problem

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I'm preparing for the scjp exam 5.0. I came across some questions asking what is the result or output of a program? How would i know a result or output of a code by mere looking or reading the code in the exam? Thanks in advance.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.
In the case of a simple program, you should be able to work out what it does just by looking. It is a case of experience and practice.
CR
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Theses questions are asking you to "think" like the compiler and the JVM (or at least know how they "think").

To think like a compiler, you should recognize obvious compilation problems -- for example, more than one public class or interface declared in the same source file, import statements appearing before package statements, trying to assign values to variables of incompatible types, variables out of scope, local variables left uninitialized, methods missing a return type, etc. At first, it might seem like a lot of syntax rules to memorize, but with experience, these things will jump out at you.

To think like the JVM, you should be able to follow the flow of execution. This usually means starting with the main method (after making sure its signature is correct!), and walking through the code exactly as the computer would. Obviously, this means understanding how flow statements work (if, while, for...), as well as exception handling. But it also means understanding the order of initialization: When classes are loaded, when static variables are initialized and static blocks execute, when supertype constructors execute, when instance variables are initialized and non-static initialization blocks execute, etc. While stepping through the code, you also need to keep track of your reference types -- especially when implicit widening and upcasting occurs.

With practice, these things will become second nature.

Do you have a specific example of a question like this? (If you post it, be sure to mention where it came from. We need to be careful that real questions don't appear here.)
[ February 18, 2007: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bettina akinsuyi:
...I'm preparing for the scjp exam 5.0...


I'll move this topic to our SCJP forum for you. Please continue this discussion there.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hate to break it to you, dear, but knowing what output a program will produce when you compile and run it is an essential part of being a programmer in any language. If your attitude is "how am I supposed to know?" then you need to reevaluate your choice of career.
 
bettina akinsuyi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr.weber,here are two questions to knowing the result and output of a code.

Firstly,
9.public void run(){
10.system.out.print ("go");

And

18.t.run();
19.t.run();
20.t.start();

what is the result?
a.go
b.go go
c.go go go
d.compilation fail
e.an exception is thrown at runtime

answer is c. i don't know why c?

secondly,
1.class Output{
2.public static void main (string[] args){
3.int i=4;
4.system.out.print("3" +i +" "){
5.system.out.print(i+4+"6");
6.system.out.println(i+"7");
what is the result?
a.7.8611
b.7.44647
c.34.8611
d.34.8647
e.34.44611
f.34.44647
the answer is d.
Pls, how d is correct.

Mr.weber i saw the 2 questions from sun microsystem scjp 5.0 e-practise.
Thanks a lot.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What did you think the answers were?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the best way for us to help you is to find out exactly where you're going wrong with questions like this. I could guess what the problems might be, but if I guess wrong, then my explanation won't help much. So please tell us how you worked through these to get your own answers.

(Note: If you're going to "think" like a compiler, then you need to be very careful about typos. For example, the compiler knows what "System" means, but not "system".)
reply
    Bookmark Topic Watch Topic
  • New Topic