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)

Practice Final Exam 3 Questions With Correct Answers

Rating
-
Sold
-
Pages
36
Grade
A+
Uploaded on
13-03-2024
Written in
2023/2024

Practice Final Exam 3 Questions) With Correct Answers Consider the Counter class below. public class Counter { public int count = 0; public int getCount() { return count; } public void increment() { count++; } } Using the class above and the variables declared below, what is the value of s(num2)? Counter num1 = new Counter(); Counter num2 = new Counter(); true false nothing since equals is not defined for Counter a syntax error Consider the following code snippet: public void deposit(double amount) { transactionCount++; it(amount); } Which of the following statements is true? This method will call itself. This method calls a public method in its subclass. This method calls a private method in its superclass This method calls a public method in its superclass. Consider the Counter class below. public class Counter { public int count = 0; public int getCount() { return count; } public void increment() { count++; } } Using the class above and the variables declared below, what is the value of s(num2)? Counter num1 = new Counter(); Counter num2 = num1; true false nothing since equals is not defined for Counter a syntax error What is a class called that represents the most general entity in an inheritance hierarchy? Default class. Superclass. Subclass. Inheritance class. Suppose the class Value is partially defined below public class Value { private int number; public int getValue() { return number; } } A subclass of Value, LargerValue, is defined with a getValue method that returns twice the value of the parent. Which line is the body of LargerValue's getValue method? return getValue() * 2; return Value() * 2; return number * 2; return r * 2; To ensure that an instance variable can only be accessed by the class that declared it, how should the variable be declared? public private protected final Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String s = String(); Assume that the Employee class has not implemented its own toString() method. What value will s contain when this code is executed? s will contain the values of the instance variables in programmer. s will contain only the class name of the programmer object. s will contain the class name of the programmer object followed by a hash code. This code will not compile. Consider the classes shown below: public class Parent { public void doSomething() // method 1 { /* Implementation not shown */ } } public class Child extends Parent { public void doSomething(int n) // method 2 { /* Implementation not shown */ } public void doSomething() // method 3 { /* Implementation not shown */ } } If the variable kid is defined below, which version of the doSomething method can be called on the variable kid? Child kid = new Child(); Methods 1 and 2 only Method 2 only Methods 2 and 3 only Methods 1, 2, and 3 Which of the following is true regarding subclasses? A subclass inherits methods from its superclass but not instance variables. A subclass inherits instance variables from its superclass but not methods. A subclass inherits methods and instance variables from its superclass. A subclass does not inherit methods or instance variables from its superclass. Why is the following code, which assumes ChoiceQuestion is a subclass of Question, considered poor strategy? if (q instanceof ChoiceQuestion) { // Do the task the ChoiceQuestion way } else if (q instanceof Question) { // Do the task the Question way } Question should be the subclass, not the other way around. instanceof is not an operator but rather a method. instanceof should not be used for type tests that can be solved by using polymorphism. the instanceof reserved word should only be used in a class constructor. nsert the missing code in the following code fragment. This fragment is intended to call the Vehicle class's method. public class Vehicle { . . . public void setVehicleClass(double numberAxles) { . . . } } public class Motorcycle extends Vehicle { . . . public Motorcycle() { _______________; } } MVehicleClass(2.0); VVehicleClass(2.0); numberAxles = 2.0; setVehicleClass(2.0); Consider the following code snippet which is supposed to show the total order amount when the button is clicked: public static void main(String[] args) { final Order myOrder = new Order(); JButton button = new JButton("Calculate"); final JLabel label = new JLabel("Total amount due"); . . . class MyListener implements ActionListener { public void actionPerformed(ActionEvent event) { Text("Total amount due " + myOAmountDue()); } } ActionListener listener = new MyListener(); } What is wrong with this code? button should be declared as final There is no error. The listener cannot access the methods of the myOrder object. The listener has not been attached to the button. What role does an interface play when using a mock class? An interface should be implemented by both the real class and the mock class to guarantee that the mock class accurately simulates the real class when used in a program. The mock class should be an interface that will be implemented by the real class. Interfaces are not involved when using mock classes. The real class should be an interface that will be implemented by the mock class. Assume that the TimerListener class implements the ActionListener interface. If the actionPerformed method in TimerListener needs to be executed once per second, what statement should be used to complete the following code segment? ActionListener listener = new TimerListener(); _________________________________ // missing statement (); Timer timer = new Timer(listener); Timer timer = new Timer(1000, listener); Timer timer = new Timer(1, listener); Timer timer = new Timer(100, listener); Consider the following code snippet: BankAccount account = new BankAccount(500); Which of the following statements correctly clones the account? BankAccount clonedAccount = O(account); BankAccount clonedAccount = (); BankAccount clonedAccount = (); BankAccount clonedAccount = (BankAccount) (); Use the ____ method to add a mouse listener to a component. addActionListener addMouseListener addMouseActionListener addListener Using the given definition of the Measurable interface: public interface Measurable { double getMeasure(); } Consider the following code snippet, assuming that BankAccount has a getBalance method and implements the Measurable interface by providing an implementation for the getMeasure method: Measurable m = new Measurable(); Sln(Measure()); Which of the following statements is true? The code executes, displaying the measure of the Measurable object. The code does not compile because interface types cannot be instantiated. The code compiles but generates an exception at run time because getMeasure does not return a String. The code compiles but generates an exception at run time because m does not reference a BankAccount object. Consider the following declarations: public interface Measurer { int measure(Object anObject); } public class StringLengthMeasurer implements Measurer { public int measure(_________________) { String str = (String) anObject; return h(); } } What parameter declaration can be used to complete the callback measure method? String aString Object aString Object anObject String anObject Which of the following is true regarding a class and interface types? You can convert from a class type to any interface type that the class extends. You cannot convert from a class type to any interface type. You can convert from a class type to any interface type that the class implements. You can convert from a class type to any interface type that is in the same package as the class. Which of the following statements about events and graphical user interface programs is true? Your program must instruct the Java window manager to send it notifications about specific types of events to which the program wishes to respond. The Java window manager will automatically send your program notifications about all events that have occurred. Your program must override the default methods to handle events. Your program must respond to notifications of all types of events that are sent to it by the Java window manager. Consider the definition of the Measurable interface and the code snippet defining the Inventory class: public interface Measurable { double getMeasure(); } public class Inventory implements Measurable { . . . public double getMeasure() { return onHandCount; } } Why is it necessary to declare getMeasure as public in the Inventory class? It is necessary only to allow other classes to use this method. It is not necessary to declare this method as public. All methods in an interface are private by default. All methods in a class are not public by default. Consider the following code snippet: myI(new Rectangle(10,10,10,10)); This code is an example of using ____. an anonymous class. an abstract class. an anonymous object. an abstract object. Which expression converts the string input containing floating-point digits to its floating-point value? Float() Double() DDouble(input) FFloat(input) Assuming that the string input contains the digits of an integer, without any additional characters, which expression obtains the corresponding numeric value? IInteger(input) IInt(input) IInt(input) Int() Which statement about handling exceptions is true? If an exception has no handler, the program will be terminated. If an exception has no handler, the error will be ignored. Statements to handle exceptions should be placed inside a try clause. Statements that may cause exceptions should be placed inside a catch clause. Consider the following code snippet: PrintWriter outputFile = new PrintWriter(filename); writeData(outputFile); outputF(); How can the program ensure that the file will be closed if an exception occurs on the writeData call? The program should place the outputF() statement within a try block to ensure that the file will be closed. The program should declare the PrintWriter variable in a try-with-resources statement to ensure that the file is closed. It is not possible to ensure that the file will be closed when the exception occurs. The program does not need to take any action, because the output file will be automatically closed when the exception occurs. If the current method in a program will not be able to handle an exception, what should be coded into the method? The method should include a try/catch block for all possible exceptions. The method declaration should be enclosed in a try/catch block. The throws clause should list the name of the method to which the exception should be passed. The throws clause should list the names of all exceptions that the method will not handle. When writing a method, which of the following statements about exception handling is true? The throws clause must list all checked exceptions that this method may throw, and may also list unchecked exceptions. The throws clause must list all checked exceptions, but cannot list unchecked exceptions. The throws clause must list all unchecked exceptions, and cannot list checked exceptions. The throws clause must list all unchecked exceptions, and may also list checked exceptions that this method may throw. Consider the following code snippet. File inputFile = new File("dataI"); Scanner in = new Scanner(inputFile); while (Next()) { String input = (); } Which of the following statements about this code is correct? This code will read in a word at a time from the input file. This code will read in a line at a time from the input file. This code will read in the entire input file in one operation. This code will read in a character at a time from the input file. Insert the missing code in the following code fragment. This code is intended to open a file and handle the situation where the file cannot be found. public void String readFile() _________________ { File inputFile = new File(. . .); try (Scanner in = new Scanner(inputFile)) { while (Next()) { . . . } } } throws FileNotFound throws IllegalArgumentException throws IOException exception throws IOException

Show more Read less
Institution
Course

Content preview

Final / Exam 3 - NR293 / NR 293 (Latest 2023 /
2024) : Pharmacology For Nursing Practice -
Chamberlain

1). Consider the counter class below.

public class counter
{
public int count = 0;


public int getcount()
{
return count;
}

public void increment()
{
count++;
}
}
using the class above and the variables declared below, what is the value of
num1.equals(num2)?

counter num1 = new counter();
counter num2 = new counter();

true

false

nothing since equals is not defined for counter

a syntax error

 Ans: false


2). Consider the following code snippet:

public void deposit(double amount)
{



PaperStoc.com Page 1 of 36

, transactioncount++;
super.deposit(amount);
}
which of the following statements is true?



this method will call itself.

this method calls a public method in its subclass.

this method calls a private method in its superclass


this method calls a public method in its superclass.

 Ans: This method calls a public method in its superclass.


3). Consider the counter class below.

public class counter
{
public int count = 0;

public int getcount()
{
return count;
}

public void increment()
{
count++;
}
}
using the class above and the variables declared below, what is the value of
num1.equals(num2)?

counter num1 = new counter();
counter num2 = num1;

true

false

nothing since equals is not defined for counter

a syntax error



PaperStoc.com Page 2 of 36

,  Ans: false


4). What is a class called that represents the most general entity in an inheritance hierarchy?



default class.

superclass.

subclass.


inheritance class.

 Ans: Superclass.


5). Suppose the class value is partially defined below

public class value
{
private int number;
public int getvalue()
{
return number;
}
}
a subclass of value, largervalue, is defined with a getvalue method that returns twice the
value of the parent. which line is the body of largervalue's getvalue method?



return getvalue() * 2;

return super.getvalue() * 2;

return number * 2;

return super.number * 2;

 Ans: return super.getValue() * 2;


6). To ensure that an instance variable can only be accessed by the class that declared it, how
should the variable be declared?




PaperStoc.com Page 3 of 36

, public


private

protected

final

 Ans: private


7). Consider the following code snippet:


employee programmer = new employee(10254, "exempt");
string s = programmer.tostring();
assume that the employee class has not implemented its own tostring() method. what
value will s contain when this code is executed?



s will contain the values of the instance variables in programmer.

s will contain only the class name of the programmer object.

s will contain the class name of the programmer object followed by a hash code.

this code will not compile.

 Ans: s will contain the class name of the programmer object followed by a hash code.


8). Consider the classes shown below:

public class parent
{
public void dosomething() // method 1
{ /* implementation not shown */ }
}

public class child extends parent
{
public void dosomething(int n) // method 2
{ /* implementation not shown */ }

public void dosomething() // method 3
{ /* implementation not shown */ }
}
if the variable kid is defined below, which version of the dosomething method can be called


PaperStoc.com Page 4 of 36

Written for

Course

Document information

Uploaded on
March 13, 2024
Number of pages
36
Written in
2023/2024
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

$12.99
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.
Academik001 Exam Questions
Follow You need to be logged in order to follow users or courses
Sold
16
Member since
3 year
Number of followers
10
Documents
2263
Last sold
1 year ago

Buy Best Exam Answers

4.0

2 reviews

5
1
4
0
3
1
2
0
1
0

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