Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Exam (elaborations)

CS2 Exam Prep with complete solutions 2022

Rating
-
Sold
-
Pages
11
Grade
A+
Uploaded on
01-08-2022
Written in
2022/2023

While the Exception class is part of , IOException is part of . True In order to have some code throw an exception, you would use which of the following reserved words? Correct Response A) throw B) Throwable C) throws D) try E) goto Throw 00:27 01:14 The Scanner class provides an abstraction for input operations by A) performing conversion operations from String to the appropriate type as specified in the Scanner message B) using try and catch statements to catch any IOException instead of throwing the Exception elsewhere C) parsing input lines into individual tokens D) inputting from the standard input stream if create is called using S Correct Response E) all of the above All of the Above Use the code below to answer the following questions. Note that the catch statements in the code are not implemented, but you will not need those details. Assume filename is a String, x is an int, a is a double array and i is an int. Use the comments i1, i2, i3, e1, e2, e3, e4, e5 to answer the questions (i for instruction, e for exception handler). try { BufferedReader infile = new BufferedReader(new FileReader(filename)); // i1 int x = IInt(Line( )); // i2 a[++i] = (double) (1 / x); // i3 } catch (FileNotFoundException ex) {...} // e1 catch (NumberFormatException ex) {...} // e2 catch (ArithmeticException ex) {...} // e3 catch (ArrayIndexOutOfBounds ex) {...} // e4 catch (IOException ex) {...} // e5 An exception raised by the instruction in i1 would be caught by the catch statement labeled A) e5 B) e1 C) e2 Correct Answer D) either e1 or e5 Incorrect Response E) either e1, e4, or e5 D) either e1 or e5 While the Exception class is part of , IOException is part of . True "class Aggregate" is incorrect. Choose the correct line so that this program prints Granite: weight=25.0 value=4 numKind=7 public class Inherit { abstract class Stone { protected float weight = 13; protected int value = 4; abstract public String toString( ); } class Aggregate { protected int numKind; } class Granite extends Aggregate { Granite( ) { weight = 25; numKind = 7; } public String toString( ) { return "Granite: weight=" + weight + " value=" + value + " numKind=" + numKind; } } Inherit( ) { Granite g = new Granite( ); Sln(g); } public static void main(String[ ] args) { new Inherit( ); } } A) abstract class Aggregate extends Granite { Incorrect Response B) class Aggregate extends Stone { C) abstract class Aggregate { Correct Answer D) abstract class Aggregate extends Stone { E) none of the above D) abstract class Aggregate extends Stone { Comparing the performance of selection sort and insertion sort, what can one say? A) The efficiencies of both sorts depend upon the data being sorted B) Insertion sort is more efficient than selection sort Correct Response C) The efficiencies of both sorts are about the same D) Selection sort is more efficient than insertion sort E) none of the above C) The efficiencies of both sorts are about the same What is printed by the following code? public class Inherit { abstract class Speaker { abstract public void speak( ); } class Cat extends Speaker { public void speak( ) { Sln("Woof!"); } } class Dog extends Speaker { public void speak( ) { Sln("Meow!"); } } Inherit( ) { Speaker d = new Dog( ); Speaker c = new Cat( ); ( ); ( ); } public static void main(String[ ] args) { new Inherit( ); } } A) Woof! Meow! B) Meow! Meow! Correct Response C) Meow! Woof! D) Woof! Woof! E) none of the above C) Meow! Woof! Comparing the amount of memory required by selection sort and insertion sort, what can one say? A) Insertion sort requires more additional memory than selection sort B) Selection sort requires more additional memory than insertion sort C) Both methods require about as much additional memory as the data they are sorting Correct Response D) Neither method requires additional memory E) None of the above D) Neither method requires additional memory The type of the reference, not the type of the object, is use to determine which version of a method is invoked in a polymorphic reference. True False False A polymorphic reference can refer to different types of objects over time. True What is printed by the following code? Consider the polymorphic invocation. public class Inherit { class Figure { void display( ) { Sln("Figure"); } } class Rectangle extends Figure { void display( ) { Sln("Rectangle"); } } class Box extends Figure { void display( ) { Sln("Box"); } } Inherit( ) { Figure f = new Figure( ); Rectangle r = new Rectangle( ); Box b = new Box( ); ay( ); f = r; ay( ); f = b; ay( ); } public static void main(String[ ] args) { new Inherit( ); } } A) Figure Figure Figure B) Rectangle Box C) Syntax error. This code won't compile or execute D) Figure Rectangle Box E) none of the above D) Figure Rectangle Box To swap the 3rd and 4th elements in the int array values, you would do: values[3] = values[4]; values[4] = values[3]; true What is printed? public class Inherit { abstract class Figure { void display( ) { Sln("Figure"); } } abstract class Rectangle extends Figure { } class Box extends Rectangle { void display( ) { Sln("Rectangle"); } } Inherit( ) { Figure f = (Figure) new Box( ); ay( ); Rectangle r = (Rectangle) f; ay( ); } public static void main(String[ ] args) { new Inherit( ); } } A) Figure Rectangle B) Rectangle Figure C) Rectangle Rectangle D) Figure Figure Incorrect Response E) none of the above C) Rectangle Rectangle Can a program exhibit polymorphism if it only implements early binding? A) No, because without late binding polymorphism cannot be supported Correct Response B) Yes, because one form of polymorphism is overloading C) Yes, because early binding has nothing to do with polymorphism D) Yes, because so long as the programs uses inheritance and/or interfaces it supports polymorphism E) none of the above B) Yes, because one form of polymorphism is overloading The statement Sln(values[7]); will A) output 7 Correct Response B) cause an ArrayOutOfBoundsException to be thrown C) cause a syntax error D) output 18 E) output nothing B) cause an ArrayOutOfBoundsException to be thrown Consider the array declaration and instantiation: int[ ] arr = new int[5]; Which of the following is true about arr? A) It stores 4 elements with legal indices between 1 and 4 B) It stores 6 elements with legal indices between 0 and 5 C) It stores 5 elements with legal indices between 1 and 5 D) It stores 5 elements with legal indices between 0 and 5 Correct Response E) It stores 5 elements with legal indices between 0 and 4 E) It stores 5 elements with legal indices between 0 and 4 Arrays have a built in toString method that returns all of the elements in the array as one String with "n" inserted between each element. False What is returned by values[3]? A) 6 B) 12 C) 3 Correct Response D) 2 E) 9 D) 2 Abstract methods are used when defining Correct Answer A) interface classes B) derived classes C) arrays D) classes that have no methods E) classes that have no constructor interface classes You may use the super reserved word to access a parent class'private members. False A is a derived class of X. False An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship? A) private B) this C) i D) inner E) static B) This Assume that the class Bird has a static method fly( ). If b is a Bird, then to invoke fly, you could do B( );. True

Show more Read less
Institution
Course

Content preview

CS2 Exam Prep
While the Exception class is part of java.lang, IOException is part of java.io. - Answer
True

In order to have some code throw an exception, you would use which of the following
reserved words?
Correct Response

A) throw

B) Throwable

C) throws

D) try

E) goto - Answer Throw

The Scanner class provides an abstraction for input operations by

A) performing conversion operations from String to the appropriate type as specified in
the Scanner message

B) using try and catch statements to catch any IOException instead of throwing the
Exception elsewhere

C) parsing input lines into individual tokens

D) inputting from the standard input stream if create is called using System.in
Correct Response

E) all of the above - Answer All of the Above

Use the code below to answer the following questions. Note that the catch statements in
the code are not implemented, but you will not need those details. Assume filename is a
String, x is an int, a is a double array and i is an int. Use the comments i1, i2, i3, e1, e2,
e3, e4, e5 to answer the questions (i for instruction, e for exception handler).

try
{
BufferedReader infile = new BufferedReader(new FileReader(filename)); // i1
int x = Integer.parseInt(infile.readLine( )); // i2
a[++i] = (double) (1 / x); // i3
}

, catch (FileNotFoundException ex) {...} // e1
catch (NumberFormatException ex) {...} // e2
catch (ArithmeticException ex) {...} // e3
catch (ArrayIndexOutOfBounds ex) {...} // e4
catch (IOException ex) {...} // e5


An exception raised by the instruction in i1 would be caught by the catch statement
labeled

A) e5

B) e1

C) e2
Correct Answer

D) either e1 or e5
Incorrect Response

E) either e1, e4, or e5 - Answer D) either e1 or e5

While the Exception class is part of java.lang, IOException is part of java.io. - Answer
True

"class Aggregate" is incorrect. Choose the correct line so that this program prints
Granite: weight=25.0 value=4 numKind=7

public class Inherit
{

abstract class Stone
{
protected float weight = 13;
protected int value = 4;
abstract public String toString( );
}

class Aggregate
{
protected int numKind;
}

class Granite extends Aggregate
{
Granite( )
{

Written for

Course

Document information

Uploaded on
August 1, 2022
Number of pages
11
Written in
2022/2023
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

$13.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
EvaTee Phoenix University
Follow You need to be logged in order to follow users or courses
Sold
5202
Member since
4 year
Number of followers
3567
Documents
55582
Last sold
6 hours ago
TIGHT DEADLINE? I CAN HELP

Many students don\'t have the time to work on their academic papers due to balancing with other responsibilities, for example, part-time work. I can relate. kindly don\'t hesitate to contact me, my study guides, notes and exams or test banks, are 100% graded

3.8

947 reviews

5
451
4
167
3
171
2
48
1
110

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions