CP213 - MCQ BANK 1. Which of the following statements are true? a) A default constructor is provided automatically if no constructors are explicitly declared in the class. b) At least one constructor must always be defined explicitly. c) Every class has a default constructor. d) The default constructor is a no-arg constructor. 2. Which of the following statements are true? a) Multiple constructors can be defined in a class. b) Constructors do not have a return type, not even void. c) Constructors must have the same name as the class itself. d) Constructors are invoked using the new operator when an object is created. e) All the above 3. Analyze the following code: public class Test { public static void main(String[] args) { A a = new A(); a.print(); } } class A { String s; A(String s) { this.s = s; } void print() { System.out.println(s); } } a) The program has a compile error because class A is not a public class. b) The program has a compile error because class A does not have a default constructor. c) The program compiles and runs fine and prints nothing. d) The program would compile and run if you change A a = new A() to A a = new A("5") 4. Analyze the following code. class TempClass { int i; public void TempClass(int j) { int i = j; } } public class C { public static void main(String[] args) { TempClass temp = new TempClass(2); } } a) The program has a compile error because TempClass does not have a default constructor. b) The program would be fine if the void keyword is removed from public void TempClass(int j) c) The program compiles fine, but it does not run because class C is not public. d) The program compiles and runs fine. 1 5. Given the declaration Circle x = new Circle(), which of the following statement is most accurate. a) x contains an int value. b) x contains an object of the Circle type. c) x contains a reference to a Circle object. d) You can assign an int value to x. 6. Analyze the following code. public class Test { int x; public Test(String t) { System.out.println("Test"); } public static void main(String[] args) { Test test = null; System.out.println(test.x); } } a) The program has a compile error because test is not initialized. b) The program has a compile error because x has not been initialized. c) The program has a compile error because you cannot create an object from the class that defines the object. d) The program has a compile error because Test does not have a default constructor. e) The program has a runtime NullPointerException because test is null while executing test.x. 7. Analyze the following code. public class Test { int x; public Test(String t) { System.out.println("Test"); } public static void main(String[] args) { Test test = null; System.out.println(test.x); } } a) The program has a compile error because test is not initialized. b) The program has a compile error because x has not been initialized. c) The program has a compile error because you cannot create an object from the class that defines the object. d) The program has a compile error because Test does not have a default constructor. e) The program has a runtime NullPointerException because test is null while executing test.x. 8. The default value for data field of a boolean type, numeric type, object type is ___________, respectively. a) true, 1, Null b) false, 0, null c) true, 0, null d) true, 1, null e) false, 1, null 2 9. Which of the following statements are true? a) Local variables do not have default values. b) Data fields have default values. c) A variable of a primitive type holds a value of the primitive type. d) A variable of a reference type holds a reference to where an object is stored in the memory. e) All the above 10. Analyze the following code: public class Test { public static void main(String[] args) { double radius; final double PI= 3.15169; double area = radius * radius * PI; System.out.println("Area is " + area); } } a) The program has compile errors because the variable radius is not initialized. b) The program has a compile error because a constant PI is defined inside a method. c) The program has no compile errors but will get a runtime error because radius is not initialized. d) The program compiles and runs fine. 11. Analyze the following code. public class Test { int x; public Test(String t) { System.out.println("Test"); } public static void main(String[] args) { Test test = new Test(); System.out.println(test.x); } } a) The program has a compile error because System.out.println method cannot be invoked from the constructor. b) The program has a compile error because x has not been initialized. c) The program has a compile error because you cannot create an object from the class that defines the object. d) The program has a compile error because Test does not have a default constructor. 12. Which of the following statements are correct? a) A reference variable is an object. b) A reference variable references to an object. c) A data field in a class must be of a primitive type. d) A data field in a class can be of an object type. 13. Which of the following statements are correct? a) When creating a Random object, you have to specify the seed or use the default seed. b) If two Random objects have the same seed, the sequence of the random numbers obtained from these two objects are identical. c) The nextInt() method in the Random class returns the next random int value. d) The nextDouble() method in the Random class returns the next random double value. e) All the above 3 14. Variables that are shared by every instance of a class are __________. a) public variables b) private variables c) instance variables d) class variables 15. A method that is associated with an individual object is called __________. a) a static method b) a class method c) an instance method d) an object method 16. To declare a constant MAX_LENGTH as a member of the class, you write a) final static MAX_LENGTH = 99.98; b) final static float MAX_LENGTH = 99.98; c) static double MAX_LENGTH = 99.98; d) final double MAX_LENGTH = 99.98; e) final static double MAX_LENGTH = 99.98; 17. Analyze the following code. public class Test { public static void main(String[] args) { int n = 2; xMethod(n); System.out.println("n is " + n); } void xMethod(int n) { n++; } } a) The code has a compile error because xMethod does not return a value. b) The code has a compile error because xMethod is not declared static. c) The code prints n is 1. d) The code prints n is 2. e) The code prints n is 3. 4 18. What is the output of the second println statement in the main method? public class Foo { int i; static int s; public static void main(String[] args) { Foo f1 = new Foo(); System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); Foo f2 = new Foo(); System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); Foo f3 = new Foo(); System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); } public Foo() { i++; s++; } } a) f2.i is 1 f2.s is 1 b) f2.i is 1 f2.s is 2 c) f2.i is 2 f2.s is 2 d) f2.i is 2 f2.s is 1 19. What code may be filled in the blank without causing syntax or runtime errors: public class Test { java.util.Date date; public static void main(String[] args) { Test test = new Test(); System.out.println(_________________); } } a) test.date b) date c) test.date.toString() d) date.toString() 20. Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class. public MyClass() { xMethod(); } a) a static method b) an instance method c) a static method or an instance method To prevent a class from being instantiated, _____________________ a) don't use any modifiers on the constructor. b) use the public modifier on the constructor. c) use the private modifier on the constructor. d) use the static modifier on the constructor. 5 21. Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } } a) The variable t is not initialized and therefore causes errors. b) The variable t is private and therefore cannot be accessed in the main method. c) t is non-static and it cannot be referenced in a static context in the main method. d) The variable x is not initialized and therefore causes errors. e) The program compiles and runs fine. 22. Analyze the following code and choose the best answer: public class Foo { private int x; public static void main(String[] args) { Foo foo = new Foo(); System.out.println(foo.x); } } a) Since x is private, it cannot be accessed from an object foo. b) Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code. c) Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code. d) You cannot create a self-referenced object; that is, foo is created inside the class Foo. 23. When invoking a method with an object argument, ___________ is passed. a) the contents of the object b) a copy of the object c) the reference of the object d) the object is copied, then the reference of the copied object 24. Given the declaration Circle[] x = new Circle[10], which of the following statement is most accurate? a) x contains an array of ten int values. b) x contains an array of ten objects of the Circle type. c) x contains a reference to an array and each element in the array can hold a reference to a Circle object. d) x contains a reference to an array and each element in the array can hold a Circle object. 25. Which of the following statements are true about an immutable object? A. The contents of an immutable object cannot be modified. B. All properties of an immutable object must be private. C. All properties of an immutable object must be of primitive types. D. A readable object type property in an immutable object must also be immutable. E. An immutable object contains no mutator methods. 6 26. You can declare two variables with the same name in __________. a) a method, one as a formal parameter and the other as a local variable b) a block c) two nested blocks in a method (two nested blocks means one being inside the other) d) different methods in a class 27. Analyze the following code: class Circle { private double radius; public Circle(double radius) { radius = radius; } } a) The program has a compile error because it does not have a main method. b) The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0. c) The program has a compile error because you cannot assign radius to radius. d) The program does not compile because Circle does not have a default constructor. 28. Which of the following statements about a class that contains an abstract method is (are) true? I. You can't have any constructors in this class. II. This class must be declared as abstract. III. You can't declare any fields in this class. a) I and II only b) II only c) I, II and III d) I only e) III only 29. Which of the following is true about abstract classes? a) Abstract classes cannot be instantiated, but they can be sub-classed. b) Abstract classes can be instantiated, but they cannot be sub-classed. c) Abstract classes can only contain abstract methods. They can be sub-classed. d) Abstract classes can only contain abstract methods. They cannot be sub-classed. 30. Which of the following option leads to the portability and security of Java? a) Bytecode is executed by JVM b) The applet makes the Java code secure and portable c) Use of exception handling d) Dynamic binding between objects 31. What is the return type of the hashCode() method in the Object class? a) Object b) int c) long d) void 7 32. Which of the following is a valid long literal? a) ABH8097 b) L990023 c) 904423 d) 0xnf029L 33. Evaluate the following Java expression, if x=3, y=5, and z=10: ++z + y - y + z + x++ a) 24 b) 23 c) 20 d) 25 34. Which of the following tool is used to generate API documentation in HTML format from doc comments in source code? a) javap tool b) javaw command c) Javadoc tool d) javah command 35. Which of the following for loop declaration is not valid? a) for ( int i = 99; i >= 0; i / 9 ) b) for ( int i = 7; i <= 77; i += 7 ) c) for ( int i = 20; i >= 2; - -i ) d) for ( int i = 2; i <= 20; i = 2* i ) 36. Which method of the Class.class is used to determine the name of a class represented by the class object as a String? a) getClass() b) intern() c) getName() d) toString() 37. Which package contains the Random class? a) java.util package b) java.lang package c) java.awt package d) java.io package 38. Which option is false about the final keyword? a) A final method cannot be overridden in its subclasses. b) A final class cannot be extended. c) A final class cannot extend other classes. d) A final method can be inherited. 39 Which keyword is used for accessing the features of a package? a) package b) import c) extends d) export 8 40. What will be the output of the following program? public class Test2 { public static void main(String[] args) { StringBuffer s1 = new StringBuffer("Complete"); s1.setCharAt(1,'i'); s1.setCharAt(7,'d'); System.out.println(s1); } } a) Complete b) Iomplede c) Cimpletd d) Coipletd 41. Given that Student is a class, how many reference variables and objects are created by the following code? Student studentName, studentId; studentName = new Student(); Student stud_class = new Student(); a) Three reference variables and two objects are created. b) Two reference variables and two objects are created. c) One reference variable and two objects are created. d) Three reference variables and three objects are created. 42. Given, ArrayList list = new ArrayList(); What is the initial quantity of the ArrayList list? a) 5 b) 10 c) 0 d) 100 43. Which statement is not true in java language? (a) A public member of a class can be accessed in all the packages. (b) A private member of a class cannot be accessed by the methods of the same class. (c) A private member of a class cannot be accessed from its derived class. (d) A protected member of a class can be accessed from its derived class. (e) None of the above. 44. To prevent any method from overriding, we declare the method as, a) static b) const c) final d) abstract e) none of the above. 45. Which one of the following is not true? a) A class containing abstract methods is called an abstract class. b) Abstract methods should be implemented in the derived class. c) An abstract class cannot have non-abstract methods. d) A class must be qualified as ‘abstract’ class, if it contains one abstract method. e) None of the above. 9 46. The fields in an interface are implicitly specified as, a) static only b) protected c) private d) both static and final e) none of the above. 47. Which of the following is not true? a) An interface can extend another interface. b) A class which is implementing an interface must implement all the methods of the interface. c) An interface can implement another interface. d) An interface is a solution for multiple inheritance in java. e) None of the above. 48. Which of the following is true? (a) A finally block is executed before the catch block but after the try block. (b) A finally block is executed, only after the catch block is executed. (c) A finally block is executed whether an exception is thrown or not. (d) A finally block is executed, only if an exception occurs. (e) None of the above. 49. What is the type and value of the following expression? -4 + 1/2 + 2*-3 + 5.0 a) int -5 b) double -4.5 c) int -4 d) double -5.0 e) None of the above. 50. Which of the following variable declaration would NOT compile in a java program? a) int var; b) int VAR; c) int var1; d) int var_1; e) int 1_var; 51. Which of the following is TRUE? a) In java, an instance field declared public generates a compilation error. b) int is the name of a class available in the package java.lang c) Instance variable names may only contain letters and digits. d) A class has always a constructor (possibly automatically supplied by the java compiler). e) The more comments in a program, the faster the program runs. 52. A constructor a) Must have the same name as the class it is declared within. b) Is used to create objects. c) May be declared private d) a and b e) a, b and c 10 53. What is byte code in the context of Java? a) The type of code generated by a Java compiler. b) The type of code generated by a Java Virtual Machine. c) It is another name for a Java source file. d) It is the code written within the instance methods of a class. e) It is another name for comments written within a program. 54. You read the following statement in a Java program that compiles and executes. submarine.dive(depth); What can you say for sure? a) depth must be an int b) dive must be a method. c) dive must be the name of an instance field. d) submarine must be the name of a class e) submarine must be a method. 55. An overloaded method consists of, a) The same method name with different types of parameters b) The same method name with different number of parameters c) The same method name and same number and type of parameters with different return type d) a and b e) a, b and c 56. In object-oriented programming, the process by which one object acquires the properties of another object is called a) Encapsulation b) Polymorphism c) Overloading d) Inheritance e) Overriding 57. _________ are used to document a program and improve its readability. a) System cells b) Keywords c) Comments d) Control structures e) Blocks. 58. Members of a class specified as_______ are accessible only to methods of that class. a) Protected b) Final c) Public d) Private e) Static 11 59. Consider the following statement(s) about Java: I. All white-space characters (blanks) are ignored by the compiler. II. Java keywords can be used as variable names. III. An identifier does not begin with a digit and does not contain any spaces. IV. The execution of Java applications begins at method main. Which of them is correct? a) Both (I) and (III) b) Both (II) and (IV) c) Both (I) and (II) d) (III) and (IV) e) All (I), (II), (III) and (IV) 60. Consider the following data types in Java : I. Int II. Boolean III. Double IV. String V. Array Which of them are simple data types? a) Both (I) and (II) b) (I), (II), (III) and (IV) (c) (I), (II) and (III) d) (II) and (III) (e) All (I), (II), (III), (IV) and (V) 61. Which will legally declare, construct, and initialize an array? a) int [] myList = {"1", "2", "3"}; b) int [] myList = (5, 8, 2); c) int myList [] [] = {4,9,7,0}; d) int myList [] = {4, 3, 7}; 62. Which one is a valid declaration of a boolean? a) boolean b1 = 0; b) boolean b2 = 'false'; c) boolean b3 = false; d) boolean b4 = Boolean.false(); e) boolean b5 = no; 63. Which is a valid declarations of a String? a) String s1 = null; b) String s2 = 'null'; c) String s3 = (String) 'abc'; d) String s4 = (String) '\ufeed'; 12 64. What is the numerical range of a char? a) -128 to 127 b) -(215) to (215) - 1 c) 0 to 32767 d) 0 to 65535 65. Which constructs an anonymous inner class instance? a) Runnable r = new Runnable() { }; b) Runnable r = new Runnable(public void run() { }); c) Runnable r = new Runnable { public void run(){}}; d) System.out.println(new Runnable() {public void run() { }}); 66. How many data types are there in java? a) 6 b) 7 c) 8 d) 9 67. Automatic type conversion in Java takes place when: a) Two type are compatible and size of destination type is shorter than source type. b) Two type are compatible and size of destination type is equal of source type. c) Two type are compatible and size of destination type is larger than source type. d) All of the above 68. Which of the following automatic type conversion will be possible? a) short to int b) byte to int c) int to long d) long to int 69. Which of the following statements is correct? a) Every line in a program must end with a semicolon. b) Every statement in a program must end with a semicolon. c) Every comment line must end with a semicolon. d) Every method must end with a semicolon. e) Every class must end with a semicolon. 70. The extension name of a Java bytecode file is a) .java b) .obj c) .class d) .exe 71. Which of the following lines is not a Java comment? a) /** comments */ b) // comments c) -- comments d) /* comments */ e) ** comments ** 13 72. If a program compiles fine, but it produces incorrect result, then the program suffers __________. a) a compilation error b) a runtime error c) a logic error 73. If you forget to put a closing quotation mark on a string, what kind error will be raised? a) a compilation error b) a runtime error c) a logic error 74. Suppose a Scanner object is created as follows: Scanner input = new Scanner(System.in); What method do you use to read an int value? a) input.nextInt(); b) input.nextInteger(); c) input.int(); d) input.integer(); 75. The following code fragment reads in two numbers: Scanner input = new Scanner(System.in); int i = input.nextInt(); double d = input.nextDouble(); What are the correct ways to enter these two numbers? a) Enter an integer, a space, a double value, and then the Enter key. b) Enter an integer, two spaces, a double value, and then the Enter key. c) Enter an integer, an Enter key, a double value, and then the Enter key. d) Enter a numeric value with a decimal point, a space, an integer, and then the Enter key. 76. To improve readability and maintainability, you should declare _________ instead of using literal values such as 3.14159. a) variables b) methods c) constants d) classes 77. Which of these data types requires the most amount of memory? a) long b) int c) short d) byte 78. If a number is too large to be stored in a variable of the float type, it _____________. a) causes overflow b) causes underflow c) causes no error d) cannot happen in Java 14 79. If you attempt to add an int, a byte, a long, and a double, the result will be a __________ value. a) byte b) int c) long d) double 80. A Java character is stored in __________. a) one byte b) two bytes c) three bytes d) four bytes 81. Which of the following statement prints smith\exam1\test.txt? a) System.out.println("smith\exam1\test.txt"); b) System.out.println("smith\\exam1\\test.txt"); c) System.out.println("smith\"exam1\"test.txt"); d) System.out.println("smith"\exam1"\test.txt"); 82. What will be displayed by System.out.println('z' - 'a')? a) 25 b) 26 c) a d) z 83. The __________ method parses a string s to an int value. a) integer.parseInt(s); b) Integer.parseInt(s); c) integer.parseInteger(s); d) Integer.parseInteger(s); 84. The __________ method parses a string s to a double value. a) double.parseDouble(s); b) Double.parsedouble(s); c) double.parse(s); d) Double.parseDouble(s); 85. In Java, the word true is ________. a) a Java keyword b) a Boolean literal c) same as value 1 d) same as value 0 86. Which of the Boolean expressions below is incorrect? a) (true) && (3 => 4) b) !(x > 0) && (x > 0) c) (x > 0) || (x < 0) d) (x != 0) || (x = 0) e) (-10 < x < 0) 15 87. Analyze the following code: if (x < 100) && (x > 10) System.out.println("x is between 10 and 100"); a) The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses. b) The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses and the println(?) statement must be put inside a block. c) The statement compiles fine. d) The statement compiles fine, but has a runtime error. 88. What is y after the following switch statement is executed? int x = 3; int y = 4; switch (x + 3) { case 6: y = 0; case 7: y = 1; default: y += 1; } a) 1 b) 2 c) 3 d) 4 e) 0 89. Analyze the following program fragment: int x; double d = 1.5; switch (d) { case 1.0: x = 1; case 1.5: x = 2; case 2.0: x = 3; } a) The program has a compile error because the required break statement is missing in the switch statement. b) The program has a compile error because the required default case is missing in the switch statement. c) The switch control variable cannot be double. d) No errors. 90. What is y after the following statement is executed? x = 0; y = (x > 0) ? 10 : -10; a) -10 b) 0 c) 10 d) 20 e) Illegal expression 91. Which of the following are valid specifiers for the printf statement? a) %4c b) %10b c) %6d d) %8.2d e) %10.2e 16 92. The statement System.out.printf("%3.1f", 1234.56) outputs ___________. a) 123.4 b) 123.5 c) 1234.5 d) 1234.56 e) 1234.6 93. The statement System.out.printf("%3.1e", 1234.56) outputs ___________. a) 0.1e+04 b) 0.123456e+04 c) 0.123e+04 d) 1.2e+03 e) 1.23+03 94. The statement System.out.printf("%5d", 123456) outputs ___________. a) 12345 b) 23456 c) 123456 d) 12345.6 95. The statement System.out.printf("%10s", "123456") outputs ___________. (Note: * represents a space) a) 123456**** b) 23456***** c) 12345***** d) ****123456 96. Suppose number contains integer value 4, which of the following statement is correct? a) System.out.printf("%3d %4d", number, Math.pow(number, 1.5)); b) System.out.printf("%3d %4.2d", number, Math.pow(number, 1.5)); c) System.out.printf("%3d %4.2f", number, Math.pow(number, 1.5)); d) System.out.printf("%3f %4.2d", number, Math.pow(number, 1.5)); e) System.out.printf("%3f %4.2f", number, Math.pow(number, 1.5)); 97. The order of the precedence (from high to low) of the operators binary +, *, &&, || is: a) &&, ||, *, + b) *, +, &&, || c) *, +, ||, && d) ||, &&, *, + 98. What is the value of the following expression? true || true && false a) true b) false 17 99. How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); count++; } while (count < 10); a) 8 b) 9 c) 10 d) 11 e) 0 100. Analyze the following statement: double sum = 0; for (double d = 0; d < 10;) { d += 0.1; sum += sum + d; } a) The program has a compile error because the adjustment is missing in the for loop. b) The program has a compile error because the control variable in the for loop cannot be of the double type. c) The program runs in an infinite loop because d<10 would always be true. d) The program compiles and runs fine. 101. The following loop displays _______________. for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++; } a) 1 2 3 4 5 6 7 8 9 b) 1 2 3 4 5 6 7 8 9 10 c) 1 2 3 4 5 d) 1 3 5 7 9 e) 2 4 6 8 10 102. What is the output for y? int y = 0; for (int i = 0; i<10; ++i) { y += i; } System.out.println(y); a) 10 b) 11 c) 12 d) 13 e) 45 18 103. Analyze the following fragment: double sum = 0; double d = 0; while (d != 10.0) { d += 0.1; sum += sum + d; } a) The program does not compile because sum and d are declared double, but assigned with integer value 0. b) The program never stops because d is always 0.1 inside the loop. c) The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers. d) After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9 104. How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) System.out.println(i * j); a) 100 b) 20 c) 10 d) 45 105. Suppose your method does not return any value, which of the following keywords can be used as a return type? a) void b) int c) double d) public e) None of the above 106. All Java applications must have a method __________. a) public static Main(String[] args) b) public static Main(String args[]) c) public static void main(String[] args) d) public void main(String[] args) e) public static main(String[] args) 107. Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion. a) a heap b) storage area c) a stack d) an array 108. Which of the following should be defined as a void method? a) A method that prints integers from 1 to 100. b) A method that returns a random integer from 1 to 100. c) A method that checks whether current second is an integer from 1 to 100. d) A method that converts an uppercase letter to lowercase. 19 109. When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as _________. a) method invocation b) pass by value c) pass by reference d) pass by name 110. A variable defined inside a method is referred to as __________. a) a global variable b) a method variable c) a block variable d) a local variable 111. Which of the following is a possible output from invoking Math.random()? a) 3.43 b) 0.5 c) 0.0 d) 1.0 112.Object-oriented programming allows you to derive new classes from existing classes. This is called ____________. a) encapsulation b) inheritance c) abstraction d) generalization 113. Which of the following is incorrect? a) A constructor may be static. b) A constructor may be private. c) A constructor may invoke a static method. d) A constructor may invoke an overloaded constructor. 114. Which of the following statements are true? a) A method can be overloaded in the same class. b) A method can be overridden in the same class. c) If a method overloads another method, these two methods must have the same signature. d) If a method overrides another method, these two methods must have the same signature. e) A method in a subclass can overload a method in the superclass. 115. The equals method is defined in the Object class. Which of the following is correct to override it in the String class? a) public boolean equals(String other) b) public boolean equals(Object other) c) public static boolean equals(String other) d) public static boolean equals(Object other) 20 116. Which of the following statements are true? a) Override the toString method defined in the Object class whenever possible. b) Override the equals method defined in the Object class whenever possible. c) A public default no-arg constructor is assumed if no constructors are defined explicitly. d) You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods. e) Only a, b and d f) All the above 117. What is the output of the following code: public class Test { public static void main(String[] args) { String s1 = new String("Java"); String s2 = new String("Java"); System.out.print((s1 == s2) + " " + (s1.equals(s2))); } } a) false false b) true true c) false true d) true false 118. Given two reference variables t1 and t2, if t1.equals(t2) is true, t1 == t2 ___________. a) is always false b) is always true c) may be true or false 119. Invoking _________ removes all elements in an ArrayList x. a) x.remove() b) x.clean() c) x.delete() d) x.empty() e) x.clear() 120. Invoking _________ returns the number of the elements in an ArrayList x. a) x.getSize() b) x.getLength(0) c) x.length(1) d) x.size() 121. What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but are accessible to any subclasses in any package? a) public b) private c) protected d) Use the default modifier. 122. The visibility of these modifiers increases in this order: a) private, protected, none (if no modifier is used), and public. b) private, none (if no modifier is used), protected, and public. c) none (if no modifier is used), private, protected, and public. d) none (if no modifier is used), protected, private, and public. 21 123. A class design requires that a particular member variable must be accessible by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this? a) The variable should be marked public. b) The variable should be marked private. c) The variable should be marked protected. d) The variable should have no special access modifier. e) The variable should be marked private and an accessor method provided. 124. Which of the following statements is false? a) A public class can be accessed by a class from a different package. b) A private method cannot be accessed by a class in a different package. c) A protected method can be accessed by a subclass in a different package. d) A method with no visibility modifier can be accessed by a class in a different package. 125. Which of the following classes cannot be extended? a) class A { } b) class A { private A();} c) final class A { } d) class A { protected A();} 126. Encapsulation means ______________. a) that data fields should be declared private. b) that a class can extend another class. c) that a variable of supertype can refer to a subtype object. d) that a class can contain another class. 127. Inheritance means ______________. a) that data fields should be declared private. b) that a class can extend another class. c) that a variable of supertype can refer to a subtype object. d) that a class can contain another class. 22