aspose file tools*
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
Author

Codes result and output problem

bettina akinsuyi
Greenhorn

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
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
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
marc weber
Sheriff

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
sscce.org
marc weber
Sheriff

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
Marshal

Joined: Jul 08, 2003
Posts: 24185
    
  34

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
Greenhorn

Joined: Feb 03, 2007
Posts: 8
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.
Keith Lynn
Ranch Hand

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

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: http://aspose.com/file-tools
 
subject: Codes result and output problem