s

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Which of the following are true about inheritance in Java? 1) Private methods are final. 2) Protected members can be accessed within a package and inheri ted classes outside the package. 3) Protected methods are final. 4) We cannot override private methods.

1, 2 and 4

class Recur { int factor(int n) { int result; if(n == 1) return 1 result = factor(n - 1) * n; return result; } } class Output { public static void main(String args[]) { Recur obj = new Recur (); System.out.print(obj.factor(5)); } }

120

What value is returned as a result of the call recur(27)?

16

int[] nums = {0, 4, 4, 5, 6, 7}; int result = bSearch(nums, 0, nums.length - 1, 4); What is the value of result after the code segment has been executed?

2

int[] nums = {10, 20, 30, 40, 50};int result = bSearch(nums, 0, nums.length - 1, 40); How many times will the bSearch method be called as a result of executing the code segment, including the initial call?

2

int[][] arr = {{1, 3, 4}, {4, 5, 3}};int max = arr[0][0]; How many times will the statement in line 11 be executed as a result of executing the code segment?

2

public static int mystery(int n) { if (n == 0) return 1; else return 3 * mystery (n - 1); } What value is returned as the result of the call mystery(5)?

243

int[] numbers = {40, 10, 20, 30}; int[] temp = new int[numbers.length]; mergeSortHelper(numbers, 0, numbers.length - 1, temp); How many times will the merge method be called as a result of executing the code segment?

3

int result = bSearch(nums, 0, nums.length - 1, -100); How many times will the bSearch method be called as a result of executing the statement, including the initial call?

4

Assume that inputList is an ArrayList of Integer objects that contains the following values: [0, 10, 30, 40, 50, 70, 70, 70, 70] What value will be returned by the call binarySearch(inputList, 0, 8, 70)?

6

How many times will "!" be printed when the code segment is executed?

6 times

Base b = new Derived(); What is the result of the call b.methodOne()?

ABDC

The code segment is intended to produce the following output. Device has memory: flash, screen area: 11.5515 square inches. Which of the following best explains why the code segment does not work as intended?

An error occurs during compilation because the getScreenWidth and getScreenHeight methods are not defined for the Computer object myPhone.

The following code segment appears in a method in a class other than Bike or EBike. EBike eB = new EBike(4); Which of the following best describes the effect of executing the code segment?

An implicit call to the zero-parameter Bike constructor initializes the instance variable numWheels. The instance variable numBatteries is initialized using the value of the parameter batteries.

What will happen if a recursive method doesn't include a base case?

An infinite loop

Once a subclass is created, no other classes can inherit from that subclass.

False

Choose the best class for which the following would be instances:goldfish, shark, tuna, swordfish.

Fish

A program is being written by a team of programmers. One programmer is implementing a class called Employee; another programmer is writing code that will use the Employee class. Which of the following aspects of the public methods and fields of the Employee class does not need to be known by both programmers?

How the methods are implemented.

Which of these constructors would be legal for the NamedPoint class?

I and III

Under which of the following conditions will the statement print "Red" ? When someApple is an object of type Apple When someApple is an object of type GrannySmith When someApple is an object of type Jonagold

I and III only

Consider the following Util class, which contains two methods. The completed sum1D method returns the sum of all the elements of the 1-dimensional array a. The incomplete sum2D method is intended to return the sum of all the elements of the 2-dimensional array m.

I, II, and III

Consider the following code segment that appears in a class other than Book or AudioBook. Line 1: Book[] books = new Book[2]; Line 2: books[0] = new AudioBook(100, 300, "The Jungle"); Line 3: books[1] = new Book(400, "Captains Courageous"); Line 4: System.out.println(books[0].pagesPerMinute()); Line 5: System.out.println(books[0].toString()); Line 6: System.out.println(books[0].length()); Line 7: System.out.println(books[1].toString()); Which of the following best explains why the code segment will not compile?

Line 4 will not compile because variables of type Book may only call methods in the Book class.

Feline tabby = new Cat(); What is printed as a result of the call tabby.speak()?

MEOWmeow

Which of the following could be used to replace / missing / so that findClosest will work as intended?

Math.abs(num - val) < minDiff

The following code segment appears in a class other than C1 or C2. C1 obj1 = new C2(); obj1.m1(); obj1.m2(); The code segment is intended to produce the output AB. Which of the following best explains why the code segment does not produce the intended output?

Method m2 is executed from the subclass instead of the superclass because obj1 is instantiated as a C2 object.

Which one of the following statements about method overloading and overriding is true?

Overriding allows for polymorphism which means that the actual method that gets called at runtime depends on the type of the object at runtime.

What is recursion in Java?

Recursion is a way of defining a method to call itself repeatedly.

class Recur { int funct(int n) { int result; result = funct(n - 1); return result; } } class Output { public static void main(String args[]) { Recur obj = new Recur(); System.out.print(obj.funct(12)); } }

Runtime error

Consider the following code segment, which is intended to declare and initialize the two-dimensional (2D) String array things. / missing code / = {{"spices", "garlic", "onion", "pepper"}, {"clothing", "hat", "scarf", "gloves"}, {"plants", "tree", "bush", "flower"}, {"vehicles", "car", "boat", "airplane"}}; Which of the following could replace / missing code / so that things is properly declared?

String[][] things

Assume that a two-dimensional (2D) array arr of String objects with 3 rows and 4 columns has been properly declared and initialized.Which of the following can be used to print the elements in the four corner elements of arr ?

System.out.print(arr[0][0] + arr[0][3] + arr[2][0] + arr[2][3]);

Bird b = new Hawk(5, 8); Which of the following best describes the effect of executing the statement?

The Bird variable b is instantiated as a Hawk. The call super(beak) invokes the Bird constructor and initializes the instance variable beakStrength with the value from the parameter beak. The instance variable talonStrength is then initialized with the value from the parameter talon.

Assume that int val has been declared and initialized with a value that satisfies the precondition of the method. Which of the following best describes the value returned by the call what(val)?

The number of digits in the decimal representation of val is returned.

Book b = new TextBook("Psychology"); Which of the following best describes the effect of executing the code segment?

There is an implicit call to the zero-parameter Book constructor. The instance variable bookTitle is then initialized to "". Then, the instance variable subject is initialized with the value of the parameter theSubject.

To call the superclass's no-parameter constructor explicitly, use super();

True

car dealership

Use one class, Car, which has three data fields: int numDoors, boolean hasAir, and double milesPerGallon.

What is printed as a result of the call whatsItDo("WATCH")?

WATC WAT WA W

The relationship between Dog and DogOwner is what object-oriented concept? public class Dog { private String name; public void setName(String n) { name = n; } public String getName() { return name; } } public class DogOwner { private String name; private Dog[] dogs; }

association

A two-dimensional array arr is to be created with the following contents. boolean[][] arr = {{false, true, false}, {false, false, true}}; Which of the following code segments can be used to correctly create and initialize arr ?

boolean arr[][] = new boolean[2][3]; arr[0][1] = true; arr[1][2] = true;

double[][] table = new double[5][6];Which of the following method calls returns a value of true ?

checkIndexes(table, 4, 5)

A superclass's constructor is inherited by its subclass.

false

The code segment is intended to print "DIG".

letters[1][0] + letters[2][2] + letters[2][0]

public class GradStudent extends Student { private String getFood() { return "Taco"; } public void teach(){ System.out.println("Education!"); getInfo(); } } What is the output from this: Student s1 = new GradStudent(); s1.teach();

not Education! would be printed, followed by a run-time error when getInfo is called. not Education! Pizza not Education! Taco

The following code segment occurs in a class other than Bike or EBike. Bike b = new EBike(250); System.out.println(b.getNumOfWatts()); System.out.println(b.getNumOfWheels()); Which of the following best explains why the code segment does not compile?

not There are too many arguments to the EBike constructor call in the code segment. not The getNumOfWatts method is not found in the Bike class.

What is printed out if we call stringMagic("Java Is Fun");?

nuF sI avaJ

Rodent rod; Rat rat; Mouse mou; LabRat lab; Which of the following code segments is correct and will not result in a compile time error?

rod = new Rat();

Dog fido = new UnderDog(); What is printed as a result of the call fido.act()?

run eat bark sleep

public boolean check(String s) { return s.length() >= 2 && (s.charAt(0) == s.charAt(1) || check(s.substring(1))); } This method will return true if and only if:

s contains two or more of the same character in a row

public class ContactInfo { private String name; private String phoneNumber; public ContactInfo(String theName, String thePhoneNumber) { this.name = theName; this.phoneNumber = thePhoneNumber; } public String getName() { return name; } public String getPhoneNumber() { return phoneNumber; } } public class ExtendedContactInfo extends ContactInfo { private String nickname; public ExtendedContactInfo (String theNickname, String theName, String thePhoneNumber) { // missing code } }

super(theName,thePhoneNumber); this.nickname = theNickname;

A subclass can make changes to the values of instance variables that belong to its superclass's private members only, through public, protected methods like "setters" inherited from the superclass and inherited into the subclass.

true

An explicit call to the superclass constructor using super() must be provided as the first statement in the subclass constructor.

true

Subclass methods can usually refer to private members of the superclass just by using the member's names.

true

int[] vals = {80, 50, 30, 20, 60, 70}; int[] temp = new int[vals.length]; mergeSortHelper(vals, 0, vals.length - 1, temp); Which of the following represents the arrays merged the last time the merge method is executed as a result of the code segment above?

{30, 50, 80} and {20, 60, 70} are merged to form {20, 30, 50, 60, 70, 80}

For example, if things contains {{"salad", "soup"}, {"water", "coffee"}}, then count(things) should return 2. For which of the following two-dimensional array input values does count NOT work as intended?

{{"scarf", "gloves", "hat"}, {"shoes", "shirt", "pants"}}

int[][] mat = new int[3][4]; What are the contents of mat after the code segment has been executed?

{{2, 1, 1, 1}, {3, 2, 1, 1}, {3, 3, 2, 1}}

For which of the following two-dimensional array input values does sumRows NOT work as intended?

{{4, 1, 7}, {-10, -11, -12}}


संबंधित स्टडी सेट्स

CompTIA A+ 220-1001 - Troubleshooting Process

View Set

focus on: understanding your sexuality

View Set

MedSurg 2 Exam 1 Study Set (ABG)

View Set

CNA - Basic Nursing Skills (Unit 2)

View Set

Bullying and Violence in the Workplace

View Set