1. What will happen when you attempt to compile and run the following code?
public class Static
{
static
{
int x = 5;
}
static int x,y;
public static void main(String args[])
{
x--;
myMethod();
System.out.println(x + y + ++x);
}
public static void myMethod()
{
y = x++ + ++x;
}
}
Choices:
a. Compiletime error
b. prints : 1
c. prints : 2
d. prints : 3
e. prints : 7
f. prints : 8
――――――――――――――――――――
1) D is the correct choice. The above code will not give any compilation error. Note that "Static" is a valid class name. Thus choice A is incorrect. In the above code, on execution, first the static variables (x and y) will be initialized to 0. Then static block will be called and finally main() method will be called. The execution of static block will have no effect on the output as it declares a new variable (int x). The first statement inside main (x--) will result in x to be -1. After that myMethod() will be executed. The statement "y = x++ + ++x;" will be evaluated to y = -1 + 1 and x will become 1. In case the statement be "y =++x + ++x", it would be evaluated to y = 0 + 1 and x would become 1. Finally when System.out is executed "x + y + ++x" will be evaluated to "1 + 0 + 2" which result in 3 as the output. Thus choice D is correct.
Considering the following code, Which variables may be referenced correctly at line 12?
1.public class Outer
2.{
3.public int a = 1;
4.private int b = 2;
5.public void method(final int c)
6.{
7.int d = 3;
8.class Inner
9.{
10.private void iMethod(int e)
11. {
12.
13.}
14.}
15.}
16.}
Choices:
a. a
b. b
c. c
d. d
e. e
A, B, C and E are correct. Since Inner is not a static inner class, it has a reference to an enclosing object, and all the variables of that object are accessible. Therefore A and B are correct, even if b is private. Variables in the enclosing method are only accessible when they are marked as final hence c is accessible but not d. E is obviously correct as it is a parameter to the method containing line 12 itself.
What will be the result of executing the following code?
// Filename; SuperclassX.java
package packageX;
public class SuperclassX
{
protected void superclassMethodX()
{
}
int superclassVarX;
}
// Filename SubclassY.java
1.package packageX.packageY;
2.
3.public class SubclassY extends SuperclassX
4.{
5.SuperclassX objX = new SubclassY();
6.SubclassY objY = new SubclassY();
7.void subclassMethodY()
8.{
9.objY.superclassMethodX();
10.int i;
11.i = objY.superclassVarX;
12.}
13.}
Choices:
a.Compilation error at line 5
b. Compilation error at line 9
c. Runtime exception at line 11
d. None of these
――――――――
D is correct. When no access modifier is specified for a member, it is only accessible by another class in the package where its class is defined. Even if its class is visible in another package, the member is not accessible there. In the question above the variable superclassVarX has no access modifier specified and hence it cannot be accessed in the packageY even though the class SuperclassX is visible and the protected method superclassMethodX() can be accessed. Thus the compiler will raise an error at line 11.
Consider the class hierarchy shown below:
--------------------------------------------------------------------
class FourWheeler implements DrivingUtilities
class Car extends FourWheeler
class Truck extends FourWheeler
class Bus extends FourWheeler
class Crane extends FourWheeler
----------------------------------------------------------------------
Consider the following code below:
1.DrivingUtilities du;
2.FourWheeler fw;
3.Truck myTruck = new Truck();
4.du = (DrivingUtilities)myTruck;
5.fw = new Crane();
6.fw = du;
Which of the statements below are true?
Choices:
a. Line 4 will not compile because an interface cannot refer to an object.
b. The code will compile and run.
c. The code will not compile without an explicit cast at line 6, because going
down the hierarchy without casting is not allowed.
d.The code at line 4 will compile even without the explicit cast.
e.The code will compile if we put an explicit cast at line 6 but will throw an exception at runtime.
―――――――――――
C and D are correct. A and B are obviously wrong because there is nothing wrong in an interface referring to an object. C is correct because an explicit cast is needed to go down the hierarchy. D is correct because no explicit cast is needed at line 4, because we are going up the hierarchy. E is incorrect because if we put an explicit cast at line 6, the code will compile and run perfectly fine, no exception will be thrown because the runtime class of du (that is Truck) can be converted to type FourWheeler without any problem.
What will be printed when you execute the following code?
class X
{
Y b = new Y();
X()
{
System.out.print("X");
}
}
class Y
{
Y()
{
System.out.print("Y");
}
}
public class Z extends X
{
Y y = new Y();
Z()
{
System.out.print("Z");
}
public static void main(String[] args)
{
new Z();
}
}
Choices:
a. Z
b. YZ
c. XYZ
d. YXYZ
―――――――――
D is correct. A difficult but a fundamental question, please observe carefully. Before any object is constructed the object of the parent class is constructed(as there is a default call to the parent's constructor from the constructor of the child class via the super() statement). Also note that when an object is constructed the variables are initialized first and then the constructor is executed. So when new Z() is executed , the object of class X will be constructed, which means Y b = new Y() will be executed and "Y" will be printed as a result. After that constructor of X will be called which implies "X" will be printed. Now the object of Z will be constructed and thus Y y = new Y() will be executed and Y will be printed and finally the constructor Z() will be called and thus "Z" will be printed. Thus YXYZ will be printed.
What will happen when you attempt to compile and run the following code?
class Base
{
int i = 99;
public void amethod()
{
System.out.println("Base.amethod()");
}
Base()
{
amethod();
}
}
public class Derived extends Base
{
int i = -1;
public static void main(String argv[])
{
Base b = new Derived();
System.out.println(b.i);
b.amethod();
}
public void amethod()
{
System.out.println("Derived.amethod()");
}
}
Choices:
a. Derived.amethod()
-1
Derived.amethod()
b. Derived.amethod()
99
c.Derived.amethod()
99
d. Derived.amethod()
e.Compile time error
――――――――
B is correct. The reason is that this code creates an instance of the Derived class but assigns it to a reference of a the Base class. In this situation a reference to any of the fields such as i will refer to the value in the Base class, but a call to a method will refer to the method in the class type rather than its reference handle. But note that if the amethod() was not present in the base class then compilation error would be reported as at compile time, when compiler sees the statement like b.amethod(), it checks if the method is present in the base class or not. Only at the run time it decides to call the method from the derived class.
Given the following code fragment:
1) public void create() {
2) Vector myVect;
3) myVect = new Vector();
4) }
Which of the following statements are true?
A. The declaration on line 2 does not allocate memory space for the
variable myVect.
B. The declaration on line 2 allocates memory space for a reference to a
Vector object.
C. The statement on line 2 creates an object of class Vector.
D. The statement on line 3 creates an object of class Vector.
E. The statement on line 3 allocates memory space for an object of class
Vector
翻译
给出下面的代码片断。。。下面的哪些陈述为true(真)?
A. 第二行的声明不会为变量myVect分配内存空间。
B. 第二行的声明分配一个到Vector对象的引用的内存空间。
C. 第二行语句创建一个Vector类对象。
D. 第三行语句创建一个Vector类对象。
E. 第三行语句为一个Vector类对象分配内存空间。
答案 A,D,E
解析
SL-275中指出:要为一个新对象分配空间必须执行new
Xxx()调用,new调用执行以下的操作:
1.为新对象分配空间并将其成员初始化为0或者null。
2.执行类体中的初始化。(例如在类中有一个成员声明int a=10;在第一步后a=0
,执行到第二步后a=10)
3.执行构造函数。
4.变量被分配为一个到内存堆中的新对象的引用。
Which of the following statements about variables and their scopes
are true?
A. Instance variables are member variables of a class.
B. Instance variables are declared with the static keyword.
C. Local variables defined inside a method are created when the method
is executed.
D. Local variables must be initialized before they are used.
(acd)
题目:下面关于变量及其范围的陈述哪些是对的。
A. 实例变量是类的成员变量。
B. 实例变量用关键字static声明。
C. 在方法中定义的局部变量在该方法被执行时创建
D. 局部变量在使用前必须被初始化。
类中有几种变量,分别是:局部变量(英文可以为:local\automatic\temporary\stac
k
variable)是定义在方法里的变量;实例变量(英文为:instance
variable)是在方法外而在类声明内定义的变量,有时也叫成员变量;类变量(英文为:cl
ass
variable)是用关键字static声明的实例变量,他们的生存期分别是:局部变量在定义该变
量的方法被调用时被创建,而在该方法退出后被撤销;实例变量在使用new
Xxxx()创建该类的实例时被创建,而其生存期和该类的实例对象的生存期相同;类变量在该
类被加载时被创建,不一定要用new
Xxxx()创建,所有该类的实例对象共享该类变量,其生存期是类的生存期。任何变量在使用
前都必须初始化,但是需要指出的是局部变量必须显式初始化,而实例变量不必,原始类型的
实例变量在该类的构造方法被调用时为它分配的缺省的值,整型是0,布尔型是false,而浮点
型是0.0f,引用类型(类类型)的实例变量的缺省值是null(没有进行实际的初始化,对它的
使用将引起NullPointException),类变量的规则和实例变量一样,不同的是类变量的初始化
是在类被加载时。
public class Parent {
int change() {…}
}
class Child extends Parent {
}
Which methods can be added into class Child?
A. public int change(){}
B. int chang(int i){}
C. private int change(){}
D. abstract int chang(){}
(ab)
题目:哪些方法可被加入类Child。
需要注意的是答案D的内容,子类可以重写父类的方法并将之声明为抽象方法,但是这引发的问题是类必须声明为抽象类,否则编译不能通过,而且抽象方法不能有方法体,也就是方法声明后面不能带上那两个大括号({}),这些D都不能满足。