Bookmark Topic Watch Topic
  • New Topic

Marcus Green Exam#3 - question #40 and #54

 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi everyone,
Question #40 says
Given the following code
class Base {
static int oak=99;
}
public class Doverdale extends Base{
public static void main(String argv[]){
Doverdale d = new Doverdale();
d.amethod();
}
public void amethod(){
//Here
}
}
Which of the following if placed after the comment //Here will compile and modify the value of the variable oak?
1.super.oak=1;
2.oak=33;
3.Base.oak=22;
4.oak=50.1;
The answer given is 1,2 and 3.
I tried this out. 2 and 3 work fine, but on 1, the compiler complains Undefined variable super. I also tried it with super() - then it complains constructors can only be called by constructors.
Is 1. a good answer or not?

Question #54
public class Inc{
public static void main(String argv[]) {
Inc inc = new Inc();
int i = 0;
inc.fermin(i);
i = i + 1;
System.out.println(i);
}
void fermin(int i) {
i++;
}
}
1.Compile time error
2.Output of 2
3.Output of 1;
4.Output of 0;

I think the answer is 3- Output of 1. I tried it, and it is correct.
The answer given is 4- Output of 0
How come?
Thanks in advance
Savithri
 
Sattiamoorthy Dessingou
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Originally posted by Savithri Devaraj:
Hi everyone,
Question #40 says
Given the following code
class Base {
static int oak=99;
}
public class Doverdale extends Base{
public static void main(String argv[]){
Doverdale d = new Doverdale();
d.amethod();
}
public void amethod(){
//Here
}
}
Which of the following if placed after the comment //Here will compile and modify the value of the variable oak?
1.super.oak=1;
2.oak=33;
3.Base.oak=22;
4.oak=50.1;
The answer given is 1,2 and 3.
I tried this out. 2 and 3 work fine, but on 1, the compiler complains Undefined variable super. I also tried it with super() - then it complains constructors can only be called by constructors.
Is 1. a good answer or not?

Question #54
public class Inc{
public static void main(String argv[]) {
Inc inc = new Inc();
int i = 0;
inc.fermin(i);
i = i + 1;
System.out.println(i);
}
void fermin(int i) {
i++;
}
}
1.Compile time error
2.Output of 2
3.Output of 1;
4.Output of 0;

I think the answer is 3- Output of 1. I tried it, and it is correct.
The answer given is 4- Output of 0
How come?
Thanks in advance
Savithri

Hi Savithri,
I don't find anything wrong with the answers.

For #40:
To call a variable from the base class and assign some value to it unless otherwise it is declared as final it is ok to use super.varname=value;
Here, super.oak=1;
Obviously in this case also it functions well.
since oak is defined as final assigning new value
oak=33;
it is also a valid declaration.
Also static varables can be called and reassigned like this
classname.varname=value;
Here, Base.oak=22;
it is also valid .

For #54:
It will output a value of 1 only.
May be wrong in the answer given.
Hope u are very well clear.
Satty
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Report post to moderator
For the second question, "i = i + 1" should really have been "i = i++". This was discussed here. I guess I should have closed thise topic at the time; anyway, I'm doing so now.
 
    Bookmark Topic Watch Topic
  • New Topic