×

Loading...

Java Quiz

What output is produced by compiling and running the following program? Note that the instance variable named x is declared private (as highlighted in boldface in the code).

A. A compiler error
B. A runtime error
C. 15
//File Q001_10.java
class Q001_10{
public static void main(
String args[]){
AClass ref1 = new AClass(5);
AClass ref2 = new AClass(10);
System.out.println(
ref1.add(ref2));
}//end main()
}//end class definition

class AClass{
private int x;

AClass(int x){//constructor
this.x = x;
}// end constructor

int add(AClass ref){
return x + ref.x;
}//end add()

}//end class AClass
Sign in and Reply Report

Replies, comments and Discussions: