Page 1 of 55 1231151 ... LastLast
Results 1 to 20 of 1093

Thread: Quiz Time

  1. #1
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Thumbs up Quiz Time

    What will be the output of follwing code:- [ don't execute it , just read and try to asnwer ]

    Java Code:
    public class ObjectTest  {	
    	ObjectTest myObject = new ObjectTest();
    	
    	public static void main(String[] args) {		
    		ObjectTest myObject = new ObjectTest();
    		System.out.println(myObject);
    	}	
    }
    i am the future

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    What ever the information the object holds. Mostly a bunch of goo.
    My IP address is 127.0.0.1

  3. #3
    fireball2008 is offline Member
    Join Date
    Apr 2008
    Posts
    28
    Rep Power
    0

    Default

    the adress of myObject in the main method!

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

    Default

    Should have stack overflow. Defining the same reference variable. If you use one of them, it gives the memory address of the object reference.
    Is that right? :)

  5. #5
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default stack overflow!!

    Eranga absolutely correct!

    actually on compilation, the code will be converted to this,

    Java Code:
    public class ObjectTest  {	
    	ObjectTest myObject;
    
         ObjectTest ()
         {
             myObject = new ObjectTest();
          }
    	
    	public static void main(String[] args) {		
    		ObjectTest myObject = new ObjectTest();
    		System.out.println(myObject);
    	}	
    }
    Hence the answer, STACK OVERFLOW

    Post some more quiz :)
    i am the future

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

  7. #7
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default Gift!

    Gift already posted to:
    Colombo, Sri Lanka :D


    More quiz please
    i am the future

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

    Default

    Ah, I'm waiting. ;)

    Here is a quick one. Don't test on IDE, just looking at give the answer.

    Java Code:
    public class MyQuiz {
    
         private int i = j;
         private int j = 10;
         
         public static void main(String args[])
         {
              System.out.println((new MyQuiz()).i);
         }
    }

  9. #9
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Unhappy Two answers!!

    Either it will successfully print 10


    Or

    it will give the error 'j is not initialized'

    or

    I don't know :D
    i am the future

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

  11. #11
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    means i am wrong :mad:
    i am the future

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

  13. #13
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    can not find symbol..... for 'j'
    something like that like that.....

    sanjeev,संजीव

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

    Default

    Ok, here is the solution.

    In such a situation comes with compilation error. It's called forward reference. That mean when you referencing the variable j in variable i, compiler can't find variable j.

    If you do it other way, like this

    Java Code:
    public class MyQuiz {
        
         private int j = 10;
         private int i = j;
    
         public static void main(String args[])
         {
              System.out.println((new MyQuiz()).i);
         }
    }
    prints 10 to the console.

  15. #15
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hey Eranga

    my answer is nearly correct......
    sanjeev,संजीव

  16. #16
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hey Eranga.....

    Really Good Question.......

    ask moreeeeeeeeeee
    sanjeev,संजीव

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

    Default

    :D

    Hmm, I'm not saying that you are wrong. But can't find symbol is something different than forward referencing. Here you can find the symbol, but can't reference.

    Something like this, you can't find the symbol.

    Java Code:
    public class MyQuiz {
    
         private int i = e;  
         private int j = 10;
         
         public static void main(String args[])
         {
              System.out.println((new MyQuiz()).i);
         }
    }
    Just compile and see the exceptions there. :)

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

    Default

    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 ;)

  19. #19
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default what's the catch

    i guess the output is:
    'yes'

    but if you are asking this in Quiz section then answer can be anything from 'no' to compilation error :D
    i am the future

  20. #20
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default gift

    send the gift or say my answer is not correct
    i am the future

Page 1 of 55 1231151 ... 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
  •