CS 2119 FINAL

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Which expression can be used to decide if x is not between 10 and 20?

!(10 < x < 20)

How is the expression evaluated? ! x - 3 > 0

((!x) - 3) > 0

Which expression fails to compute the area of a triangle having base b and height h (area is one-half base time height)?

(1 / 2) * b * h

What is the output? for (i=0; i<10; ++i) { if (i==6) continue; else System.out.print(i + " "); }

0 1 2 3 4 5 7 8 9 10

What is the ending value of sum, if the input is 2 5 7 3? All variables are integers. x = scnr.nextInt(); sum = 0; for (i = 0; i<x; ++i){ currValue = scnr.nextInt(); sum += currValue; }

12

What is y after executing the statements? x = 5; y = x+1; y = y*2;

12

What is the final value of y? int x = 6; int y = 2; if(x<10){ if(y<5){ y = y+1; } else { y = 7; } } else { y = y+10; }

3

Which expression is evaluated first? w = y + 2 + 3 * x + z;

3 * x

How many x's will be output? i = 1; while (i <= 3) { j = 1; while (j<= i) { System.out.print("x"); ++j; } System.out.println(); ++i; }

6

What are the return values for the method calls, mScore(27, 89, 15) and mScore('d', 'e', 'f')? public class SortScores { public static (T scr1, T scr2, T scr3) { T matchScore = scr1; if (scr2.compareTo (matchScore) >0) { matchScore = scr2; } if (scr3.compareTo (matchScore) >0) { matchScore = scr3; } return matchScore; } }

89 and f

The correct syntax for declaring a type parameter is _____.

<The Type extends Comparable<TheType>>

What type of a class has been defined in the following code: public class StudentIDs <TheType extends Comparable<TheType>>{ private TheType item1; private TheType item2; public StudentIDs(TheType i1, TheType i2) { item1 = i1; item2 = i2; } }

A comparable class

What does the compiler do upon reaching this variable declaration? int x;

Allocates a memory location for x

When declaring an ArrayList collection class variable the angle bracket operators are employed in the declaration as in the sample below. From the choices below, select the choice which best describes the angle bracket operator and its functionality. ArrayList<String> myStrings = newArrayList<>();

Angle brackets are used for declaring Java generics and tells the compiler to ensure only Strings are collected by the object.

Match each of the terms on the left below with the best definition / description from the choices on the right. Break Continue While Loop Do-While Loop For Loop

Break: terminates the enclosing control statement and operation flow continues following the enclosing control statement Continue: terminates the current iteration of encoding control statement and operational flow advances to the next iteration While loop: an iterative control statement with the evaluation expression performed at the top of the loop Do-while loop: an iterative control statement with the evaluation expression performed at the bottom of the loop For loop: an iterative control statement with the initialization, evaluation, and step expressions specified in the control statement

Which capability is not provided by data structures?

Data Transmission

Which is true?

Data written to System.out are placed in a buffer and eventually output

What is the output? public static void main(String[] args) { for ( int i = 0; i < 3; ++i) { System.out.print(i); } System.out.print(i); }

Error: The variable i is not declared in the right scope

For the code sample obj1 and obj2 are independent and unique object instances. Not only are obj1 and obj2 distinct object references, but the object instances they reference are unique and independent object instances because Java does a deep copy when the assignment is made.

False

True of False: Most, if not all, iterative or repetitive algorithms can be written with a while, a do-while or a for loop in Java.

False

True or False: A nested Java class can only be accessed by the enclosing class regardless of the declaration of the inner class.

False

True or False: For a Java ArrayList object, the size is the number of items the collection can hold and the capacity is the number of items currently in the collection.

False

True or False: For each Java class or object you write in your code you must include a toString method. No default behavior is provided by the Java runtime environment for printing objects or object references.

False

True or False: If you pass the name of an existing valid file to the PrintWriter constructor, any text written to the file will be appended to the end of the existing file.

False

True or False: Java arrays can only be used to store Java primitives; to store Java objects a developer needs to use an ArrayList.

False

True or False: Java classes can only implement a single interface since Java only allows single inheritance

False

True or False: Java enums must be instantiated in the main method of a Java program to ensure the enum is created and instantiated by the constructor before any reference is made to the enum. public void main (String [ ] args) { String obj1 = new String("This is a Java String"); }

False

True or False: The Java compiler will accept the overloading of the getResults method because each version specifies a different formal parameter name which makes the signatures unique and the compiler can distinguish the intent of the calling code and call the correct version of the method.

False

True or False: Unlike single dimension arrays, multidimensional arrays in Java cannot be initialized when declared. Java loops must be used to set the array values cell by cell based on loop indexes.

False

What is used to determine the type of element held by a Collection?

Generic Type

What does this code output? System.out.println("I "); System.out.println("want pie.");

I want pie.

What is the general purpose of a collection?

It groups objects together into a single object that provides common operations for working with the group.

Two approaches a programmer could employ to manage collections of objects with an ArrayList are: 1. Treat each of the objects added or inserted into the ArrayList as an Object. This can be done since each object inherits from the root Object in Java. 2. Instantiate the ArrayList leveraging Java Generics so the ArrayList instance is customized to the specific class that the programmer needs to manage. The second approach is often (nearly always) the better. From the choices below, select all that identify advantages of the second approach.

Leveraging generics to customize the ArrayList for specific class type allows ArrayList to store Java primitives without need for autoboxing and unboxing

A class that implements an interface must implement the interface's _____.

Methods (An interface defines the methods that must be implemented on a class.)

Which input for char c causes "Done" to be output next? c = 'y'; while (c='y'){ // do something System.out.println("Enter y to continue, n to quit"); // get c from input } System.out.println("Done");

No such value (error comparing c and 'y')

It groups objects together into a single object that provides common operations for working with the group.

Position

What is a best practice for determining the variable type when writing a declaration statement for a Collection?

Prefer the interface type over the concrete class.

The Java wrapper classes (such as Character, Integer, Double, etc.) provide which functionality?

Provide automatic boxing and unboxing without any performance cost Wrapper objects allow primitives to be passed by reference to methods Allows Java primitives to be stored in Java collection classes such as ArrayLists

What is the correct syntax to define a generic class with multiple bounded parameters?

Public class MyClass <Type1 extends ParType1, Type2 extends ParType2> {...}

What does the String.split() method do?

Returns an array of Strings by splitting the String instance according to a delimiter

When constructing complex strings in stages by adding multiple character sequences or modifying existing character sequences, the Java StringBuilder class can often perform better than operations on the String class. Why?

StringBuilder manages memory more efficiently than String does

What advantage does a collection have over an array?

The size of the collection adjusts automatically as we add new elements.

Given the two alternate versions of a Java for loop above, where the only difference is the update expression uses pre-increment operator in Example A and post-increment operator in Example B, which of the following statements is true? A: int ar[ ] = {2, 4, 6, 8}; int sum = 0; for (int index = 0; index < ar.length(); ++index) { sum += ar[index]; B: int ar[ ] = {2, 4, 6, 8}; int sum = 0; for (int index = 0; index < ar.length(); index++) { sum += ar[index];

There is no difference in the calculation of the value of sum because the step expression always completes before the test expression is evaluated

What is the purpose of a variable?

To hold a value

True or False: In Java, two dimensional arrays are arrays of arrays.

True

True or False: Java wrapper classes are immutable. One the wrapper object is created its value cannot be changed.

True

A generic class has a special _____ that can be used in place of types in the class.

Type parameter

From the choices below, select all which are true statements

a. An interface can only have method bodies for default methods b. Java allows classes to implement multiple interfaces

What is the name of a class that adheres to an interface's contract?

an implementations

Which declaration assigns a variable with 5 such that the value can be later changed?

final int NUM_ITEMS = 5;

For JFrame frame, which syntax terminates the program properly once the GUI window is closed?

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Identify the correct statement for JFrame myFrame to set the height to 400 and width to 200.

myFrame.setSize(200,400);

Which XXX is the proper default constructor? public class Employee { private double mySalary; XXX }

public Employee(){ mySalary = 10000; }

Select the default constructor signature for a Student class.

public Student( )

Which XXX completes the definition of the generic method, avgNum? public class FindAverage { XXX{ long tripleSum; tripleSum = item.longValue() + item2.longValue() + item3.longValue(); return tripleSum / 3; } }

public static <TheType extends Number> long avgNum(TheType item1, TheType item2, TheType item3)

What are the fill colors of shape1, shape2, and shape3? Color col1 = newColor(255, 0, 0); graphicsObj.setColor(col1); graphics.Object.fill(shape1); Color col2 = newColor(0, 255, 0); graphicsObj.setColor(col2); graphics.Object.fill(shape2); Color col3 = newColor(0, 0, 255); graphicsObj.setColor(col3); graphics.Object.fill(shape3);

shape1: red shape2: green shape3: blue

Which statement creates a JFrame titled "Percentage Calculator" and assigns it to topFrame?

topFrame = new JFrame("Percentage Calculator");

A program should compute two times x. Which statement has a logic error?

y = x * x;


Kaugnay na mga set ng pag-aaral

Finding Slope/Y-Int. from Standard and Slope-Intercept Form

View Set

Chapter 8 - Supporting Your IdeasAssignment

View Set

Chapter Exam: Georgia Laws and Rules

View Set

ATI Pharm Made Easy Cardiovascular System

View Set

Praxis II physics (5265) Part 2: Electricity and Magnetism

View Set