File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Codes result and output problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Codes result and output problem" Watch "Codes result and output problem" New topic

Codes result and output problem

bettina akinsuyi

Joined: Feb 03, 2007
Posts: 8
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.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 36514
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.
marc weber

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
marc weber

Joined: Aug 31, 2004
Posts: 11343

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.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24166

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.

[Jess in Action][AskingGoodQuestions]
bettina akinsuyi

Joined: Feb 03, 2007
Posts: 8,here are two questions to knowing the result and output of a code.

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


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

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

1.class Output{
2.public static void main (string[] args){ i=4;
4.system.out.print("3" +i +" "){
what is the result?
the answer is d.
Pls, how d is correct. i saw the 2 questions from sun microsystem scjp 5.0 e-practise.
Thanks a lot.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
What did you think the answers were?
marc weber

Joined: Aug 31, 2004
Posts: 11343

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".)
I agree. Here's the link:
subject: Codes result and output problem
Similar Threads
Simple Math Expression
Codes result and output problem
Marcus Exam 1
help me the code
when args.length is checked whats the output?