Q. What is written to the monitor by the following section of code: String strA
ID: 3853737 • Letter: Q
Question
Q. What is written to the monitor by the following section of code: String strA = new String("Roasted "); String strB = new String("Acorns "); strA = strB; if ( strA == strB ) System.out.print("Two copies of a reference."); else System.out.print("Two different references."); A. Two copies of a reference. B. Two different references. C. Two copies of a reference. Two different references. D. Nothing is printed. Q. Examine the following section of code: String strA = new String("Roasted "); strA = new String("Toasted "); strA = new String("Fried "); strA = new String("Baked "); strA = new String("Beans "); Q. How many objects (total) are created? After the last statement has executed, how many objects are now accessible (don't count garbage)? A. This section of code is incorrect. B. created: 5 now accessible: 5 C. created: 1 now accessible: 1 D. created: 5 now accessible: 1 What is the effect of giving a class member private access? A. When a member of a class is declared private it can be used in only one place in a program. B. When a member of a class is declared private it can be used only in methods that are members of that class. C. When a member of a class is declared private it can only be used by other private members of other classes. D. When a member of a class is declared private there will be only one instance of it, no matter how many objects are instantiated. Q. What is an abstract class? A. An abstract class is one without any child classes. B. An abstract class is any parent class with more than one child class. C. An abstract class is class which cannot be instantiated, but can be a base class. D. abstract class is another name for "base class." Q. You want to create a table that looks like: 12 -9 8 7 14 -32 -1 0 Q. Which of the following will work? A. double[][] table = { 12, -9, 8, 7, 14, -32, -1, 0} ; B. double[][] table = { {12, -9, 8}, {7, 14, 0}, -32, -1, 0} }; C. double[][] table = { {12, -9, 8} {7, 14} {-32, -1, 0} }; D. double[][] table = { {12, -9, 8}, {7, 14}, {-32, -1, 0} }; Q. Given: int[][] items = { {0, 1, 3, 4}, {4, 3, 99, 0, 7 }, {3, 2} } ; Which of the following fragments replaces row 0 of items with an entierly new row? A. items[0][0] = 8; items[0][1] = 12; items[0][2] = 6; B. items[0] = { 8, 12, 6 }; C. items[0] = new { 8, 12, 6 }; D. int[] temp = { 8, 12, 6 }; items[0] = temp; Q. What must a non-abstract child do about an abstract method in its parent class? A. A child must override an abstract method inherited from its parent by defining a method with the same signature and same return type. B. A child must define an additional method similar to the one inherited from its parent by defining a method with the same signature and different return type. C. A child must not define any method with the same signature as the parent's abstract method. D. A non-abstract child must define an abstract method with the same signature and same return type as the parent's abstract method. Q. Can an interface name be used as the type of a variable, like this: public static void main( String[] args ) { SomeInterface x; ... } A. No—a variable must always be an object reference type. B. No—a variable must always be an object reference type or a primitive type. C. No—a variable must always be a primitive type. D. Yes—the variable can refer to any object whose class implements the interface. Q. Examine the following fragment: String cat = "Hello" cat += " "; cat += "World" ; How many objects are constructed? A. 0 B. 1 C. 2 D. 3 Q. For each of the following code snippets, please indicate if the code shown would a. fail to compile, b. compile, but result in an error at runtime, or c. execute without an error. In this case, indicate what output you would see. public abstract class Plant { public void smell() { System.out.println(“No smell”); } } public class Flower extends Plant { public void smell() { System.out.println(“I smell good!”); } } public class Fruit extends Plant { public void smell() { System.out.println(“I smell yummy!”); } } a. Flower rose = new Flower(); rose.smell(); Ans: b. Fruit orange = new Fruit(); orange.smell(); Ans: c. Plant oakTree = new Plant(); oakTree.smell(); Ans: d. Plant banana = new Fruit(); banana.smell(); Ans: e. Plant violet = new Flower(); Flower myViolet = violet; myViolet.smell(); Ans: Q. Consider the following two classes: public class ClassA { public void methodOne(int i) { } public void methodTwo(int i) { } public static void methodThree(int i) { } public static void methodFour(int i) { } } public class ClassB extends ClassA { public static void methodOne(int i) { } public void methodTwo(int i) { } public void methodThree(int i) { } public static void methodFour(int i) { } } Question 1a: Which method overrides a method in the superclass? Answer 1a: Question 1b: Which method hides a method in the superclass? Answer 1b: Question 1c: What do the other methods do? Answer 1c: Q. Consider the following classes to trace the output: public class A{ A(){ System.out.println(“A”); } } public class B extends A{ B(){ System.out.println(“B”); } } public class C extends B{ C(){ System.out.println(“C”); } } public class Tester{ public static void main(String [] args){ A a = new A(); C c = new C(); B b = new B(); } } Output: Q. Consider the following classes to trace the output: interface Testable { abstract public void test(); } abstract class A { public void inA(){ System.out.println("In a method"); } abstract public void method1(); } public class HelloWorld extends A implements Testable{ public void method1(){ System.out.println("In method1"); } public void test(){ System.out.println("delicious!"); } public static void main(String [] args){ A a = new HelloWorld(); a.method1(); a.inA(); Testable t = (HelloWorld)a; t.test(); } } Output:
Explanation / Answer
Q. What is written to the monitor by the following section of code:
String strA = new String("Roasted ");
String strB = new String("Acorns ");
strA = strB;
if ( strA == strB )
System.out.print("Two copies of a reference.");
else
System.out.print("Two different references.");
A. Two copies of a reference. B. Two different references. C. Two copies of a reference. Two different references. D. Nothing is printed.
Answer is A as String assignment assign reference of a string
Q. Examine the following section of code:
String strA = new String("Roasted ");
strA = new String("Toasted ");
strA = new String("Fried ");
strA = new String("Baked ");
strA = new String("Beans ");
How many objects (total) are created? After the last statement has executed, how many objects are now accessible (don't count garbage)?
A. This section of code is incorrect. B. created: 5 now accessible: 5 C. created: 1 now accessible: 1 D. created: 5 now accessible: 1
Answer is D as String is immutable only one instance will remain there and hence accessible and as 5 times string object is created
What is the effect of giving a class member private access?
A. When a member of a class is declared private it can be used in only one place in a program.
B. When a member of a class is declared private it can be used only in methods that are members of that class.
C. When a member of a class is declared private it can only be used by other private members of other classes.
D. When a member of a class is declared private there will be only one instance of it, no matter how many objects are instantiated.
Answer is B.
Q. What is an abstract class?
A. An abstract class is one without any child classes.
B. An abstract class is any parent class with more than one child class.
C. An abstract class is class which cannot be instantiated, but can be a base class.
D. abstract class is another name for "base class."
Answer is C,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.