Declarations & Access Control1

  • Uploaded by: rahul rastogi
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Declarations & Access Control1 as PDF for free.

More details

  • Words: 3,321
  • Pages: 15
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com

Declarations & Access Control Setâ–º1 Question 1 package com.dan.chisholm; public class A { public void m1() {System.out.print("A.m1, ");} protected void m2() {System.out.print("A.m2, ");} private void m3() {System.out.print("A.m3, ");} void m4() {System.out.print("A.m4, ");} } class B { public static void main(String[] args) { A a = new A(); a.m1(); // 1 a.m2(); // 2 a.m3(); // 3 a.m4(); // 4 }} Assume that the code appears in a single file named A.java. What is the result of attempting to compile and run the program?

a. Prints: A.m1, A.m2, A.m3, A.m4, d. Compile-time error at 3. b. Compile-time error at 1. e. Compile-time error at 4. c. Compile-time error at 2. f. None of the above Answer: Both class A and B are declared in the same package, so class B has Compile-time access to the public, protected, and package access methods of d error at 3. class A.

Question 2 class MWC101 { public static void main(String[] args) { int[] a1 = new int[]; // 1 int a2[] = new int[5]; // 2 int[] a3 = new int[]{1,2}; // 3 int []a4 = {1,2}; // 4 int[] a5 = new int[5]{1,2,3,4,5}; // 5 }} Compile-time errors are generated at which lines?

a. b. c. d. e.

1 2 3 4 5

1

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com Answer: An array creation expression must have either a dimension expression or an initializer. If both are present, then a compile-time error is generated. Similarly, if neither is present, then a compile-time error is generated. If only the dimension expression is present, then an array with the specified dimension is created with all a elements set to the default values. If only the initializer is present, then an array will e 1 be created that has the required dimensions to accommodate the values specified in 5 the initializer. Java avoids the possibility of an incompatible dimension expression and initializer by not allowing both to appear in the same array creation expression. A compile-time error is generated by the array creation expression for a1, because it needs either a dimension expression or an initializer. A compile-time error is generated at 5, because either the dimension expression or the initializer must be removed.

Question 3 class MWC201 { public static void main(String[] args) { int[][] a1 = {{1,2,3},{4,5,6},{7,8,9,10}}; System.out.print(a1[0][2]+","+a1[1][0]+","+a1[2][1]); }} What is the result of attempting to compile and run the program?

a. b. c. d. e.

Prints: 3,4,8 Prints: 7,2,6 Compile-time error Run-time error None of the above

2

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com Answer: An array variable a1 is declared, and the declaration contains the initializer {{1,2,3},{4,5,6},{7,8,9,10}}. The initializer creates an array Prints: containing three components, and each is a reference to a subarray of type a 3,4,8 int[]. Each subarray contains components of type int, so the elements of the array referenced by a1 are of type int. The array access expression, a1[0] [2] = a1[1st subarray][third component] = 3.

Question 4 Which of the following modifiers can be applied to the declaration of a field? a. b. c. d. e.

abstract final private protected public

3

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com Answer: A field is a class member. A static field is sometimes called a class variable. A non-static field is sometimes called an instance variable. A variable declaration that is immediately contained by a b final block such as a method body is called a local variable. The access private c modifiers, private, protected and public, can be applied d protected to a field. A final field can not have its value assigned more e public than once. The abstract modifier may be applied to methods but not to fields.

Question 5 Which of the following modifiers can not be applied to a method? a. b. c. d. e. f.

abstract private protected public volatile None of the above.

4

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com Answer: An abstract method declaration provides no method body. If one abstract method is appears within a class declaration, then the entire class must be declared abstract and the class can not be instantiated. e volatile The access modifiers, private, protected and public, can be applied to a method. The field modifiers, transient and volatile, are not applicable to method declarations.

Question 6 class JSC201 { static byte m1() { final char c1 = '\u0001'; return c1; // 1 } static byte m2(final char c2) {return c2;} // 2 public static void main(String[] args) { char c3 = '\u0003'; System.out.print(""+m1()+m2(c3)); // 3 }} What is the result of attempting to compile and run the program?

a. Prints: 13 d. Compile-time error at 2 b. Prints: 4 e. Run-time error c. Compile-time error at 1 f. None of the above Answer: d CompileThere is a compile-time error at 2. The char type variable c2 is not a time error at compile-time constant, so it can not be assigned to type byte without an 2 explicit cast. The method parameter c2 is declared final, so the value of c2 can not be changed within method m2. The value of method parameter c2 is set at run time to the value of the argument that is provided when m2 is invoked at line 3. For that reason, the method parameter c2 is not a compile-time constant. In method m2, the statement, "return c2;", is a return statement with an expression, c2. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of method m2 is byte. The return statement attempts to return the value of the char type variable c2. If a char value is a compile-time constant, and if the value falls within the range of type byte, then the char value is assignable to type byte. In method m2, variable c2 is not a compile-time constant, because the value of c2 is not known at compile time. Instead, the value of c2 is assigned at run time to the value of the argument. Since the char type variable c2 is not a compile-time constant, the value of variable c2 is not assignable to the return type of method m2 without an explicit cast. While the declaration of method m2 produces a compile-time error, the declaration of method m1 does not. The local variable c1 is declared final and the value is set at compile time; so c1 is a compile-time constant. The value \u0001 falls within the range of type byte; so the value of the compile-time constant c1 is assignable to the return type of method m1 without an 5

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com explicit cast.

Question 7 Which of the following modifiers can be applied to a class that is not a nested class? a. public b. protected c. private Answer:

a public d abstract f final

d. abstract e. static f. final The access modifiers, protected and private, can be applied to a class that is a member of an enclosing class, but can not be applied to a local class or a class that is not nested inside another class. The static modifier can be applied to a class that is a member of an enclosing class, but can not be applied to a local class or a class that is not nested inside another class. The public modifier can be applied to a top level class to allow the class to be accessed from outside of the package. The abstract modifier prevents the class from being instantiated. An abstract class may include zero, one or more abstract methods. The final modifier prevents a class from being extended.

Question 8 class A {A(int i) {}} class B extends A {}

// 1 // 2

Which of the following statements are true? a. b. c. d.

The compiler attempts to create a default constructor for class A. The compiler attempts to create a default constructor for class B. Compile-time error at 1. Compile-time error at 2.

Answer: b The compiler attempts d to create a default constructor for class B. Compile-time error at 2.

If no constructor is declared explicitly, then the compiler will implicitly create a default constructor that accepts no parameters, has no throws clause, and invokes its superclass constructor. Since class A has an explicitly declared constructor, the compiler will not create an implicit default constructor. Class B does not have an explicit constructor declaration, so the compiler attempts to create a default constructor. Since class A does not have a no-parameter constructor, the attempt by class B to invoke the no parameter constructor of A would fail. As a result, a compiler error is 6

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com generated at marker 2.

Question 9 Which of the following modifiers can be applied to a constructor? a. b. c. d. e. f.

private abstract final volatile native None of the above.

7

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com Answer: Constructors are not inherited and can not be overridden, so there is no need for the final modifier in a constructor declaration. Furthermore, an abstract constructor would be useless, since it could never be a private implemented. The volatile modifier can be applied to a field, but not to a constructor. Native constructors are not permitted, because it would be difficult for Java to verify that the native constructor properly invokes the superclass constructor.

Question 10 class MWC102 { public static void main (String[] args) { byte[] a = new byte[1]; long[] b = new long[1]; float[] c = new float[1]; Object[] d = new Object[1]; System.out.print(a[0]+","+b[0]+","+c[0]+","+d[0]); }} What is the result of attempting to compile and run the program?

a. b. c. d. e.

Prints: 0,0,0,null Prints: 0,0,0.0,null Compile-time error Run-time error None of the above

8

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com Answer: Each array contains the default value for its type. The default value of a primitive byte or a primitive long is printed as 0. The default value of Prints: b 0,0,0.0,null a float primitive is printed as 0.0. The default value of an Object is null and is printed as null.

Question 11 class MWC202 { public static void main(String[] args) { int[] a1[] = {{1,2},{3,4,5},{6,7,8,9}}; int []a2[] = {{1,2},{3,4,5},{6,7,8,9}}; int a3[][] = {{1,2},{3,4,5},{6,7,8,9}}; System.out.print(a1[0][1]+","+a2[1][2]+","+a3[2][3]); }} What is the result of attempting to compile and run the program?

a. Prints: 14 d. Prints: 2,4,8 g. Run-time error b. Prints: 16 e. Prints: 2,5,9 h. None of the above c. Prints: 1,5,9 f. Compile-time error Answer: The declarations of the array variables, a1, a2 and a3, are different in terms of the position of the square brackets, but each is of type int[][]. The array access expression, a1[0][1] = a[1st subarray][2nd component] = 2. Each of the three array variable declarations has an array initializer. The Prints: e same initializer, {{1,2},{3,4,5},{6,7,8,9}}, is used in each of the 2,5,9 three cases. The initializer specifies three components of type int[], so each component is a reference to an array object containing components of type int. The size of each of the subarrays is different: The size of the first is 2, the second is 3, and the third is 4.

Question 12 Which of the following modifiers can be applied to the declaration of a field? a. b. c. d. e.

static synchronized transient volatile native

9

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com Answer: A transient field is not part of the persistent state of an object. Transient fields are not serialized. Fields that are shared between a static threads may be marked volatile to force each thread to reconcile c transient its own working copy of the field with the master copy stored in the d volatile main memory. The synchronized modifier may be applied to methods but not to fields.

Question 13 Which of the following modifiers can not be applied to a method? a. b. c. d. e. f.

final static synchronized transient native None of the above.

10

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com Answer: A final method can not be overridden. A static method is associated with a class, but not a particular instance of the class. A thread can not enter a d transient synchronized method without first acquiring a lock. The field modifiers, transient and volatile, are not applicable to method declarations. A native method is implemented in platform-dependent code.

Question 14 class JSC202 { static byte m1() {final short s1 = 2; return s1;} // static byte m2(final short s2) {return s2;} // public static void main(String[] args) { short s3 = 4; System.out.print(""+m1()+m2(s3)); // }} What is the result of attempting to compile and run the

1 2 3 program?

a. Prints: 24 d. Compile-time error at 2. b. Prints: 6 e. Run-time error c. Compile-time error at 1. f. None of the above Answer: There is a compile-time error at 2. The short type variable s2 is not a compiletime constant, so it can not be assigned to type byte without an explicit cast. The method parameter s2 is declared final, so the value of s2 can not be changed within method m2. The value of method parameter s2 is set at run time to the value of the argument that is provided when m2 is invoked at line 3. For that reason, the method parameter s2 is not a compile-time constant. In method m2, the statement, "return s2;", is a return statement with an expression, s2. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of method m2 is byte. The return statement attempts to return the value of the short type Compilevariable s2. If a short value is a compile-time constant, and if the value falls d time error at within the range of type byte, then the short value is assignable to type byte 2. without an explicit cast. In method m2, variable s2 is not a compile-time constant, because the value of s2 is not known at compile time. Instead, the value of s2 is assigned at run time to the value of the argument. Since the short type variable s2 is not a compile-time constant, the value of variable s2 is not assignable to the return type of method m2 without an explicit cast. While the declaration of method m2 produces a compile-time error, the declaration of method m1 does not. The local variable s1 is declared final and the value is set at compile time; so s1 is a compile-time constant. The value 2 falls within the range of type byte; so the value of the compile-time constant s1 is assignable to the return type of method m1 without an explicit cast.

Question 15 Which of the follow statements is true. 11

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com a. An anonymous class can be declared abstract. b. A local class can be declared abstract. c. An abstract class can be instantiated. d. An abstract class is implicitly final. e. An abstract class must declare at least one abstract method. f. An abstract class can not extend a concrete class. Answer: An anonymous class can not be extended; therefore, an anonymous class can not be declared abstract. A local class can be abstract. An A local class can abstract class can not be instantiated. If a class declaration contains an b be declared abstract method, then the class must also be declared abstract. A abstract. class can be declared abstract even if it does not contain an abstract method. An abstract class can never be declared final.

Question 16 Which of the following techniques can be used to prevent the instantiation of a class by any code outside of the class? a. Do not declare any constructors. b. Do not use a return statement in the constructor. c. Declare all constructors using the keyword void to indicate that nothing is returned. d. Declare all constructors using the private access modifier. e. None of the above. Answer: If no constructors are declared explicitly; then the compiler will create one implicitly, and it will have the same access modifier as the class. The explicit declaration of any constructor will prevent the creation of a Declare all constructors using the default constructor. If all constructors are declared private, then code d private access outside of the class will not have access to the constructors and will not have the ability to create an instance of the class. Constructors do not modifier. return a value and constructor declarations do not include a return type, so the keyword void is not applicable to a constructor declaration.

Question 17 Which of the following modifiers can be applied to a constructor? a. protected b. public c. static Answer: a protected

d. synchronized e. transient

Constructors can not be inherited, so an abstract constructor would be 12

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com useless, since it could never be implemented. A static constructor would also be useless--or nearly so--since it would be unable to access the non-static members of the new instance. An object is not available to multiple threads b public during the construction process, so the synchronized modifier would not provide additional protection. The transient modifier can be applied to a field, but not a constructor.

Question 18 // Class A is declared in a file named A.java. package com.dan.chisholm; public class A { public void m1() {System.out.print("A.m1, ");} protected void m2() {System.out.print("A.m2, ");} private void m3() {System.out.print("A.m3, ");} void m4() {System.out.print("A.m4, ");} } // Class D is declared in a file named D.java. package com.dan.chisholm.other; import com.dan.chisholm.A; public class D { public static void main(String[] args) { A a = new A(); a.m1(); // 1 a.m2(); // 2 a.m3(); // 3 a.m4(); // 4 }} What is the result of attempting to compile and run the program?

a. Prints: A.m1, A.m2, A.m3, A.m4, d. Compile-time error at 3. b. Compile-time error at 1. e. Compile-time error at 4. c. Compile-time error at 2. Answer: Classes A and D are not declared in the same package, so class D c Compile-time error at 2. does not have access to package access method, m4. Since class D d Compile-time error at 3. does not extend class A, class D does not have access to the e Compile-time error at 4. protected method, m2, of class A.

Question 19 class MWC103 { public static void main(String[] args) { int[] a1 = {1,2,3}; int []a2 = {4,5,6}; int a3[] = {7,8,9}; System.out.print(a1[1]+","+a2[1]+","+a3[1]); }} What is the result of attempting to compile and run the program?

a. b. c. d.

Prints: 12 Prints: 15 Prints: 18 Prints: 1,4,7

e. f. g. h. 13

Prints: 2,5,8 Prints: 3,6,9 Compile-time error Run-time error

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com i. None of the above Answer: Arrays a1, a2 and a3 all contain 3 integers. The first element of the array has an Prints: e 2,5,8 index of 0 so an index of one refers to the second element of each array.

Question 20 class MWC203 { public static void main(String[] args) { int[] a1[] = {new int[]{1,2},new int[]{3,4,5}}; int []a2[] = new int[][]{{1,2},{3,4,5}}; int a3[][] = {{1,2},new int[]{3,4,5}}; System.out.print(a1[0][1]+","+a2[1][0]+","+a3[1][2]); }} What is the result of attempting to compile and run the program?

a. b. c. d. e. f. g.

Prints: 14 Prints: 16 Prints: 1,2,4 Prints: 2,3,4 Prints: 2,3,5 Prints: 2,4,5 Compile-time error h. Run-time error i. None of the above

14

Rahul Rastogi: www.faqworldofrastogi.wetpaint.com Answer: Each of the three array variable declarations, a1, a2 and a3, is different in terms of the position of the square brackets, but each declares a variable of type int[][]. Each of the three declarations contains an array initializer. In each case, the initializer Prints: could be simplified as follows: {{1,2},{3,4,5}}. The initializer creates an array e 2,3,5 containing two components, and each is a reference to an array containing components of type int. The size of each of the subarrays is different: The size of the first is 2 and the second is 3. The array access expression, a1[0][1] = a[1st subarray] [2nd component] = 2.

15

Related Documents


More Documents from ""