Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

questions overriding , operators  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi javaranchers , please tell the answer of following and why ?
public class test {
public static void main(String args[]) {
int i=0, j=2;
do {
i=++i;
j--;
} while(j>0);
System.out.println(i);
}
}
What all gets printed when the following program is compiled and run. Select all correct answers.
0
1
2
The program does not compile because of statement "i=++i;"
*****************************************************************
If a base class has a method defined as void method() { }
Which of the following are legal prototypes in a derived class of this class. Select all correct answers.
void method() { }
int method() { return 0;}
void method(int i) { }
private void method() { }
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For question 1, when I run it I get 2. This is because the System.out.println(i) statement does not execute until AFTER the loop, at which point i = 2.
For the second question, I say 1 and 3 are the correct answers. The 2nd answer has a different return type from the superclass method, so will not compile, while the 4th answer attempts to make the method more private, which cannot be done.
HTH
Adam
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm, I think
i=++i;
is the same as written:
++i;
right??
 
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nils Widmer:
hmm, I think
i=++i;
is the same as written:
++i;
right??


Yes, but i=i++; is not the same thing as i++;
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Asish,
For code 1 :
The out put will be 2.
For code 2 :
The valid methods in the sub class are
void method() { }
void method(int i) { }
Indu
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try changing the line from:
i=++i;
to
i=i++;
and see the difference. Any lights???
--Farooq
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
******************************************
If a base class has a method defined as void method() { }
Which of the following are legal prototypes in a derived class of this class. Select all correct answers.
void method() { }
int method() { return 0;}
void method(int i) { }
private void method() { }
*****************************************************
Isn't the answer just the first one?
The second definition is obviously illegal because of the different return type.
The third method wouldn't serve as a prototype, but rather an overloaded method when the subclass inherits it.
The private modifier on the fourth method would prevent it from getting inherited, thus disqualifying it from being a prototype.

What does everybody else think?
 
Jimmy Blakely
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh cr@p...my bad.
I misconstrued the wording. It was asking for legal prototypes in the subclass, not base class.
With that correction in mind, I would go with the first and third ones.
Sowee about that.
 
Pay attention! Tiny ad!
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!