Page 45 of 55 FirstFirst ... 354344454647 ... LastLast
Results 881 to 900 of 1093

Thread: Quiz Time

  1. #881
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  2. #882
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Eranga View Post
    Oops, you are wrong. You can compile the code without any error at all in the code.
    Code in white, so as not to be a spoiler. (Drag your mouse over it to see it.)
    Java Code:
    myhost:~ user$ vim TestWhile.java
    
    public class TestWhile {
        public static void main(String[] args) {
            try {
                if (3 >= 3) {
                    while (true) // missing a semicolon here?
                } else {
                    System.exit(0);
                }
            } finally {
                System.out.println("final");
            }
        }
    }
    
    myhost:~ user$ javac TestWhile.java
    TestWhile.java:6: illegal start of expression
                } else {
                ^
    TestWhile.java:6: 'else' without 'if'
                } else {
                  ^
    TestWhile.java:9: 'finally' without 'try'
            } finally {
              ^
    TestWhile.java:3: 'try' without 'catch' or 'finally'
            try {
            ^
    TestWhile.java:13: reached end of file while parsing
    }
     ^
    5 errors


    -Gary-
    Last edited by Eranga; 03-25-2010 at 04:26 AM.

  3. #883
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Oh, no. I'm missing a part of it. Shall I edit the question, since not too many comments here.

  4. #884
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Edit or repost, either way is fine. :)

    -Gary-

  5. #885
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #886
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    I'll let somebody else answer because I couldn't resist checking before posting. :D

    -Gary-

  7. #887
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    i'm just reading the tutorial about assertions. perhaps this is an easy quiz for the scjp cracks but i try anyway

    Java Code:
    public class OldCode {
    	static boolean assert = true;
    
    	public static void main(String[] args) {
    		System.out.println(assert);
    	}
    }
    how to compile the above code with java 6 without errors?

  8. #888
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by gcalvin View Post
    I'll let somebody else answer because I couldn't resist checking before posting. :D

    -Gary-
    :) Sorry pal, is I make you upset there. Then I'll post another one soon.

  9. #889
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Quote Originally Posted by Eranga View Post
    Java Code:
    try {
            if (3 >= 3) {
                 while (true) {
                 }
            } else {
                System.exit(0);
            }
    }
    finally {
        System.out.println("final");
    }
    [/COLOR]
    Does the phrase "compiler optimisation" come into this, by any chance?

  10. #890
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  11. #891
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Well, it plays a part in that I would expect the "if (3 >=3)" would be optimised away, leaving simply the while loop. Which means the System.exit is pointless since it won't be turned into byte code. So, essentially, you have an infinite loop.

  12. #892
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  13. #893
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Since 3 >= 3 is true, it's going to loop, so the final block will not execute, and nothing will print. Otherwise, System.exit(0) would end the JVM, so the finally would not execute, and nothing would print.
    The Java Tutorial. Read it.

  14. #894
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  15. #895
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Finally, I get one right!
    The Java Tutorial. Read it.

  16. #896
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Steve11235 View Post
    Finally, I get one right!
    So our gift is, post a quiz from you. We are waiting. ;)

  17. #897
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default My "gift"

    I'm embarrassed to even admit I made up such a kluge (pronounced clue-gie) piece of code.

    Now for my question. What prints, or am I bluffing?
    Java Code:
    package test;
    
    public class QuizTime {
    
    	public static void main(String[] args) {
    		Extend extend = new Extend();
    		Extend.ExtendInner extendInner = extend.new ExtendInner();
    		((Base) extend).doStaticProcess();
    	}
    }
    
    class Base {
    	
    	static Base baseInstance;
    	
    	static void doStaticProcess() {
    		System.out.println("Base doStaticProcess: " + baseInstance.doInstanceProcess());
    	}
    	
    	BaseInner innerInstance;
    	
    	Base() {
    		baseInstance = this;
    	}
    	
    	String doInstanceProcess() {
    		return "Base doInstanceProcess.";
    	}
    	
    	class BaseInner {
    		
    		BaseInner() {
    			innerInstance = this;
    		}
    		
    		String doInstanceProcess() {
    			return "BaseInner doInstanceProcess: " + innerInstance.doInstanceProcess();
    		}
    	}
    }
    
    class Extend extends Base {
    
    	static void doStaticProcess() {
    		System.out.println("Extend doStaticProcess: " + baseInstance.doInstanceProcess());
    	}
    
    	Extend() {
    	}
    
    	String doInstanceProcess() {
    		return "Extend doInstanceProcess: " + innerInstance.doInstanceProcess();
    	}
    
    	class ExtendInner extends Base.BaseInner {
    
    		ExtendInner() {
    		}
    
    		String doInstanceProcess() {
    			return "ExtendInner doInstanceProcess.";
    		}
    	}
    }
    The Java Tutorial. Read it.

  18. #898
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Wink

    Quote Originally Posted by Eranga View Post
    Ok, here is another one. :)

    Java Code:
    public class MyQuizTwo {
        
        public static void main(String[] args) {
            if("String".trim() == "String".trim())
                System.out.println("Yes");
            else
                System.out.println("No");
    
    
        }
    }
    Guess the answer, not in an IDE ;)
    And what happened to equals() method? :rolleyes:

  19. #899
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    1. True, it will run
    2. The final block will not execute because the loop never ends
    3. Nothing will be printed to the console. The inner while loop will suck a CPU "forever" (or for about 3 seconds until you manually kill the process)

  20. #900
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Page 45 of 55 FirstFirst ... 354344454647 ... LastLast

Similar Threads

  1. Hello, first time here.
    By ludragon in forum Introductions
    Replies: 2
    Last Post: 01-03-2008, 05:03 AM
  2. Help pls with a quiz
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 06:09 AM
  3. Time method
    By carderne in forum New To Java
    Replies: 5
    Last Post: 11-05-2007, 09:34 AM
  4. DataObject with the time given by me
    By garinapavan in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 06:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •