2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Which of the following restrictions apply to method overriding?

A final method may not be overridden. The overriding and overridden methods must have the same return types. The access of the overriding method must not be more restrictive than the overridden method. The overriding method must not throw exceptions that are not thrown by the overridden method.

You should add the static keyword in the place of ? in Line ________ in the following code: 1 public class Test { 2 private int age; 3 4 public ? int square(int n) { 5 return n * n; 6 } 7 8 public ? int getAge() { 9 } 10}

A. in line 4 B. in line 8 C. in both line 4 and line 8 D. none The correct answer is A Explanation: The square method should be static because it does not reference any instance data or invoke any instance method.

Which of the following is true about overloaded methods?

The correct answer is A. Overloaded must have the same name but different parameter lists.

class Question { public static void main(String[] args) { Class2 v2 = new Class2(); v2.display(); } } class Class1 { String s = "Class1"; void display() { System.out.println(s); } } class Class2 extends Class1 { String s = "Class2"; }

The correct answer is A. The display() method of Class1 is invoked and it displays the s variable of Class1.

Which expressions are always true for the following code? String s = new String("true"); boolean t = true; Please select all the correct answers. A. s.equals(""+t) B. t.equals(s) C. t == s D. s == ""+t

The correct answer is A. The expression ""+t results in the creation of a String object with a value of "true." The equals() method will always return true when two String objects have equal values.

int y = 3; y++; x = y++; A. 3 B. 4 C. 5 D. 6

The correct answer is B. The initial value of y is 3. It is then incremented by 1. The value of 4 is assigned to x before y is incremented again.

What is the output of the following program? class Question{ static String ThisIsATest; public static void main(String[] args){ String s = "\"" + ThisIsATest + "\""; System.out.println(s); } }

The correct answer is C. The variable ThisIsATest is initialized to the null value. The \" is an escape code for ".

class Question { public static void main(String[] args) { System.out.println("\\\"\\\\\""); } }

The correct answer is C. \\ is the escape code for \ and \" is the escape code for ". C. \"\\"

Which expressions are always true for the following code? String s = "123"; String t = new String("123"); Please select all the correct answers. A. s.equals(t) B. t.equals(s) C. t == s D. s == t

The correct answers are A and B. The equals() method will always return true when two String objects have equal values.

Which of the following may be declared as static?

The correct answers are A,C and D. A. Field variables C. Methods D. Initializer Local variables may not be declared as static.

What is the output of the following program? public class Question { public static void main(String args[]){ String[] s = new String[2]; boolean[] b = new boolean[2]; for(int i=0;i<2;++i) System.out.print(s[i]+b[i]); } }

nullfalsenullfalse The correct answer is C. The default value of the String type is null. The default value of the boolean type is false.

The && operator does not evaluate its second operand if the value of the first operand is false.

true


Ensembles d'études connexes

Chapter 14: Consumer Decision Process And Problem Recognition

View Set

MEDIDAS DE TENDENCIA CENTRAL (EJERCICIOS)

View Set

FORMS OF OWNERSHIP - OBJECTIVE 1 - DEFINE SOLE PROPRIETORSHIP AND EXPLAIN THE SIX ADVANTAGES AND SIX DISADVANTAGES OF THIS OWNERSHIP MODEL

View Set

Human development quiz 8 (chapter 21)

View Set

finance 4020 exam 2 practice exam!!! (math only)

View Set

Teaching a Patient to Use an Incentive Spirometer

View Set

TST 102 Fundamentals of Test and Evaluation

View Set

Foundations of Business Analytics Exam 3

View Set

Accident and Health Insurance Basics

View Set