CS1410

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

Which of the following are syntactically valid Java identifiers?

$isYourFriend HeWouldLike25Pancakes _underscores_are_fun_said_no_one_ever temperature MyClass

What is the minimum number of times a while loop can iterate?

0

How many unique Apple objects will exist after executing the following java code? Apple one = new Apple(); Apple two = one;

1

What is the minimum number of times a do-while loop can iterate?

1

Which statements below (identified by the comments) will be executed in the following Java program? public class DivisionFun {public static void main(String[] args) {try {int valueOne = 123; // 1 int valueTwo = 0; // 2 int result = divideNumbers(valueOne, valueTwo); // 3 System.out.println("Result: " + result); // 4 }catch (ArithmeticException e) {System.out.println("Attempted divide by 0 - shame!"); // 5 }System.out.println("Leaving main..."); // 6 }public static int divideNumbers(int first, int second) {return first / second; }}

1,2,3,5,6

For a single exception handling structure, which of the following statements are true.

A catch (Exception ex) block will handle all exceptions derived from the Exception class A checked exception must be handled The order catch statements are written matters

Given the following abstract class definition, which of the following items must be included by a concrete subclass which derives from Bird in order to compile? public abstract class Bird {public Bird(double weight) {this.weight = weight; }public double getWeight() {return weight; }public void setWeight(double weight) {this.weight = weight; }public abstract boolean canFly(); public abstract String makeSound(); protected double weight;}

A constructor that calls the Bird constructor using super An implementatioon of the makeSound() method An implemetation of the canFly() method

The this keyword is...

A hidden parameter passed to all instance methods that refers the class instance itself.

The following code is an example of: int [][] array = {{1}, {2,3}, {4,5,6}};

A ragged array

Which of the following are true about inheritance in Java (select all that apply)

A subclass can override a public method from a superclass Private methods in a super class are not accessible in a subclass

What type of relationship is represented by the following code? class Pilot{ . . . Pilot code here . . . } class Airplane{ public void addPilot(Pilot toAdd){ . . . add to array here . . . } private Pilot[] pilots; . . .other Airplane code here. . . }

Aggregation

Which of the following best represents the type of class relationship expressed by the following statements? A Course is made up of many Students Students have a Mailing Address A Car has a Driver

Aggregation (ownership)

Under which condition is a finally{} block executed?

Always

Which of the following is the correct way to declare and create an instance of an ArrayList that will work with the following code? items.add(1); items.add(10); items.add(100);

Arraylist<Integer> items = new ArrayList<>();

Which of the following best represents the type of class relationship expressed by the following statements? Students take Courses Faculty teach Courses People walk their Dogs

Association

The following code works in java because.. int y = 10; Integer x = y;

Auto-boxing of primitive types

What is output to the console when running the Java program show below? BankAccount.java: public class BankAccount {public BankAccount(double startBalance) {balance = startBalance; }public double getBalance() { return balance; }public void setBalance(double newBalance) { balance = newBalance; }private double balance;} Bank.java: public class Bank {public static void main(String[] args) {BankAccount myAccount = new BankAccount(10.0); closeAccount(myAccount); System.out.println("Balance: " + myAccount.getBalance()); }public static void closeAccount(BankAccount account) {account.setBalance(0.0); }}

Balance .0.0

What does the following Java code do? int[] first = { 1, 2, 3, 4, 5 }; int[] second = { 1, 2, 3, 4, 5 }; first = second;

Causes 'first' to be an alias or another way to reference the 'second' array

Which of the following best represents the type of class relationship expressed by the following statements? A Student has an A# A Hand has 5 Fingers A Plane has a Cockpit

Composition (exclusive ownership)

What is the primary purpose of the Java Development Kit (JDK)?

Contains the Java compiler and libraries needed to develop Java programs

Select the answer that best applies to the following code segment... Circle[] array = new Circle[10];

Creates an array of references to Circle objects, initially set to null.

Top-Down, or Step-wise, Design is...

Decomposition of a problem into manageable sub-problems, then potentially decomposing those sub-problems into yet more sub-problems

Match the terms below with their definitions: Overloading Methods: Overriding Methods:

Defining multiple methods with the same name but different signatures Providing a new implementation for a method in a subclass

What is wrong with the following string? String somethingNotRightHere = "Can you find "it"?";

Double quotes must be escaped within a string

Immediately upon allocation of an array of objects, the default constructor is called for each object in the array. MyObjects[] array = new MyObjects[4];

False

Java only supports a single and two-dimensional array. No further dimensions are possible.

False

Method parameters in Java can be passed by reference.

False

To make accessing them easier, all variables within classes should always be declared with public visibility

False

When returning a two-dimensional array from a method, the return type must be short one set of brackets, because the method only returns a reference. Example: int[] generateArray() { return new int[3][3]; }

False

For the Car class defined below, what is the result and rationale of comparing the two Car objects created in main using the '==' operator? public class Car {public static void main(String[] args) {Car carOne = new Car("Chevy", "Camaro", 2017, "Red"); Car carTwo = new Car("Chevy", "Camaro", 2017, "Red"); boolean sameCars = (carOne == carTwo); // Result??? Why? }public Car(String make, String model, int year, String color) {this.make = make; this.model = model; this.year = year; this.color = color; }private String make; private String model; private int year; private String color;}

False - each variable references a different Car object

What is output by the following Java program? public class LogicalOps { public static void main(String[] args) { int pizzas = 1; int cansOfDew = 5; boolean happiness = (pizzas >= 2) || (cansOfDew > 4); System.out.println("Happy?: " + happiness); } }

Happy?:true

Which of the following are true statements

If used for constructor chaining, the super keyword must be the first statement in a subclass constructor The Object class defines a default equals() method

Which of the following demonstrates the concept of auto-boxing primitive data types in Java?

Integer x = 42;

The Triceratops and TyrannosaurusRex classes extend the Dinosaur base class. With that knowledge, what does the following code output to the console? public class DinosaurDriver { public static void main(String[] args) { Dinosaur toDisplay = new TyrannosaurusRex(); displayDino(toDisplay); } public static void displayDino(Dinosaur dino) { if (dino instanceof Triceratops) { System.out.println("Triceratops here!"); } else if (dino instanceof TyrannosaurusRex ) { System.out.println("My short arms make me sad..."); } else if (dino instanceof Dinosaur) { System.out.println("I'm a dinosaur!"); } } }

My short arms make me sad...

The following driver code is an example of what concept? Fruit[] basket = new Fruit[3];basket[0] = new Banana();basket[1] = new Apple();basket[2] = new Pear();for (Fruit fruit : basket) {System.out.println(fruit.describe());}

Polymorphism

In a switch statement, what determines when to stop executing code once a matching case is found?

Reaching a break statement

With java.util.Scanner imported ina program, which proper java syntax for creating a java.util.scanner object for getting user input from the console?

Scanner input = new Scanner(System.in);

What character is used to end Statements in Java?

Semicolon

which of the following best describes the intent of interfaces in java?

Specify common behavior for objects or related or unrelated classes

What is wrong with the Java ArrayList usage below? ArrayList<Integer>ages; ages.add(10); ages[0] = 100;

The ArrayList object must be created using the 'new' operator before use ArrayList elements are accessed using the get() public method, not array subscripts

What is output to the console by the following Java program? public class ComputeArea { public static void main(String[] args) { double length = 5.0; double height = 2.0; double area = length * height; System.out.println("The area for the rectangle (" + length + " by " + height + ") is: " + area); } }

The area for the rectable (5.0 by 2.0) is 10.0

Which of the following is a prerequisite to performing a binary search?

The data set must be sorted

Which statements are true about a try{} block? (select all that apply)

The statements in a try{} block may throw several types of exceptions. Some of the statements in a try{} block will never throw an exception The try{} block must be placed before the catch{} blocks

All Exceptions provided by the Java SDK have Throwable as a superclass.

True

Every Java program conatins at least 1 class

True

Modifications made to the contents of an array, which is passed-by-value, as a parameter to a method persist after the method completes.

True

The Object class is the root of all classes

True

What is output by the following formatted print statement? String formatString = "[%5d%-10d]"; System.out.printf(formatString, 42, 100);

[ 42100 ]

What is the purpose of the JAva Runtime Environment(JRE)?

`Executes Java bytecode, allowing you to run compiled Java programs on any platform

A blueprint, template, or contract which defines data fields and methods is a ..

class

If a TieFighter class were to inherit from the StarFighter class defined below, which parts of the StarFighter class would be accessible to the TieFighter class? (select all that apply) public class StarFighter {public StarFighter(double mass, double velocity, int passengers) {this.mass = mass;this.velocity = velocity;this.passengers = passengers;}private double calculateMomentum() {return mass * velocity;}public double mass;private double velocity;private int passengers;}

double mass StarFighter(double mass, double velocity, int passengers)

Which of the following is an example of a static method call?

double num = Math.random();

A class in Java can only implement a single interface

false

A ragged array is an array that has uninitialized data.

false

In Java, a two-dimensional array may hold two-different primitive data types.

false

Returning an object from a method creates and returns a new identical copy of that object to the caller of the method

false

Which keyword is used to declare a named constant in Java?

final

For an array whose type is int[][], which of the following code segments is valid

for (int[] row : array) { System.out.println(row); }

Which of the following (select all that apply) show valid creation and use of an array of 5 integers in Java?

int[] myInts = new int[5]; myInts[3] = 42; int[] myInts = { 1, 2, 3, 4, 5 }; myInts[0] = 42;

Which of the following code segments correctly declare and create complete two-dimensional arrays (select all that apply).

int[][] array = new int[5][5]; int[][] array = { { 1, 2, 3 }, { 4, 5 } , { 6 }};

Which of the following are valid methods for initializing a two-dimensional array? (select all that apply)

int[][] array= {{1, 2}, {3, 4}}; int[][] array= {{1, 2}, {3, 4, 5}};

Which of the following is the proper way to create a two-dimensional array?

int[][] square = new int[5][5];

Which of the following are valid multi-dimensional array declarations? (select all that apply)

int[][][][] array; int[][][][][] array; int[][][] array; int[][] array;

which of the following operators instantiates an object in java?

new

Which of the following are valid Java method headers; everything except for the body (select all that apply).

public static void callMe() public static int callMe(String one, double two)

Which method signature is recognized as the entry point for Java programs?`

public static void main(String[] args)

What is the purpose of the 'this' keyword in Java?

remove ambiguity between variables within the class and parameter names passed to methods and constructors Reference the object itself from within a class

A reference variable that does not point to an instantiated object is known as a null reference

true

An abstract class can never be instantiated

true


Ensembles d'études connexes

Nursing Care of Children ATI Practice A

View Set

comfort and pain/ sleep and rest

View Set

Chapter 6: Obsessive Compulsive Disorder

View Set

CHAPTER 14: [PNS] PERIPHERAL NERVOUS SYSTEM

View Set

Business Finance Mid-Term Study Guide:

View Set

Accounting Quiz 1 (Chapter 1 and 2)

View Set

Pre-Licensing Insurance Course Chapter 19 (Part 1)

View Set