CSE Quizzes 4-10

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

To define a class that will represent a car, which of the following definitions is most appropriate?

public class Car

Given the following: int[][] table; which of the following statement constructs a table with 2 rows and 5 columns?

table= new int[ 2 ][ 5 ];

The statement int[ ] list = {5, 10, 15, 20};

initializes list to have 4 int values

The following code accomplishes which of the tasks written below? Assume list is an int array that stores positive int values only. int foo = list[0]; for (int j =1 ; j < list.length; j++) if (list[j] > foo) foo = list[j];

it stores the largest value in list (the maximum) in foo

Given the following: int[ ][ ] list = { {23,43,36,51} , {12,9,5,4} , {77,84,24,12} , {15,91,83,76} }; which of the following statement replaces 77 with 22?

list[ 2 ][ 0 ] = 22;

Which of the following reserved words in Java is used to create an instance of a class?

new

In Java, arrays are:

objects

The relationship between a class and an object is best described as

objects are instances of classes

To print out the last element of an array named arr, the following statement can be used: System.out.println(arr[arr.length]);

False

Java arrays can store primitive types and Strings, but cannot store any other type of Object other than Strings.

Fasle

Use the following class definition: public class Swapper { private int x; private String y; public int z; public Swapper(int a, String b, int c) { x = a; y = b; z = c; } public String swap( ) { int temp = x; x = z; z = temp; return y; } public String toString( ) { if (x < z) return y; else return "" + x + z; } } If the instruction Swapper s = new Swapper(0, "hello", 0); is executed followed by s.toString( ); what value is returned from s.toString( )?

"00"

Use the following class definition: public class Swapper { private int x; private String y; public int z; public Swapper(int a, String b, int c) { x = a; y = b; z = c; } public String swap( ) { int temp = x; x = z; z = temp; return y; } public String toString( ) { if (x < z) return y; else return "" + x + z; } } If we have Swapper r = new Swapper (5, "no", 10); then r.swap( ); returns which of the following?

"no"

If an int array is passed as a parameter to a method, which of the following would adequately define the parameter list for the method header?

(int [ ] a)

For an initialization of an array: int[] x = new int[100]; What is the value (currently) of x[23]?

0

What's the output of the following statement? int [] numbers = {12,18,94,71,63}; int [] list = new int [5]; numbers = list; System.out.println( numbers[3] );

0

What is the output of the following program? int[] a = new int[5]; a[0] = 0; for (int i = 1; i <= 4; i++) { a[i] = a[i-1] + i; System.out.print(a[i] + " "); }

1 3 6 10

Assume that BankAccount is a predefined class and that the declaration BankAccount[ ] firstEmpireBank; has already been performed. Then the following instruction reserves memory space for firstEmpireBank = new BankAccount[1000];

1000 reference variables, each of which point to a single BankAccount entry

Assume values is an int array that is currently filled to capacity, with the following values: 9 4 12 2 6 8 18 What is returned by values[3]?

2

Assume values is an int array that is currently filled to capacity, with the following values: 9 4 12 2 6 8 18 what is the value of values.length?

7

What is a mutator method?

A method that modifies a value

If int[ ] x = new int[15]; and the statement x[-1] = 0; is executed, then which of the following Exceptions is thrown?

ArrayIndexOutOfBoundsException

Assume values is an int array that is currently filled to capacity, with the following values:

Cause an ArrayIndexOutOfBoundsException to be thrown

In Java, "instantiation" means

Creating a new a object

Assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye". The expression (s.concat(t).length( ) < y) is true.

False

Java arrays are categorized as primitive data type.

False

Linear search algorithm is more efficient than binary search.

False

Assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all. What does the following method do? public int quiz( ) { int value1 = 0; int value2 = 0; for (int j = 0; j < 12; j++) if (candy[j] > value1) { value1 = candy[j]; value2 = j; } return value2; }

It returns the index of the child who sold the most candy bars

Consider the array declaration and instantiation: int[ ] arr = new int[5]; Which of the following is true about arr?

It stores 5 elements with legal indices between 0 and 4

The behavior of an object is defined by the object's

Methods

In the following class, the method setQuantity is a public class Shop { private int quantity; public void setQuantity(int q) { quantity = q; } }

Mutator

Consider the following class definition: import java.text.DecimalFormat; public class Student { private String name; private String major; private double gpa; private int hours; public Student(String newName, String newMajor, double newGPA, int newHours) { name = newName; major = newMajor; gpa = newGPA; hours = newHours; } public String toString( ) { DecimalFormat df = new DecimalFormat("0.000"); return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours; } } Which of the following could be used to instantiate a new Student s1?

Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33);

Use the following class definition: public class Swapper { private int x; private String y; public int z; public Swapper(int a, String b, int c) { x = a; y = b; z = c; } public String swap( ) { int temp = x; x = z; z = temp; return y; } public String toString( ) { if (x < z) return y; else return "" + x + z; } } Which of the following criticisms is valid about the Swapper class?

The instance data z is visible outside of Swapper

An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship?

This

A constructor is a method that gets called whenever an object is created (instantiated) , for example with the new operator.

True

A constructor must have the same name as its class.

True

A method can have only one return type. If the method is not returning anything then it must be void

True

The Binary Search does not work correctly unless the array is sorted.

True

The Maximum number of comparisons the linear search performs in searching a value in array of size x is equals to x.

True

What is the return type of a method that does not returns any value?

Void

Which of the following will yield a pseudorandom number in the range [ -5, +5 ) given the following: (include -5 but exclude 5) Random gen = new Random( );

gen.nextFloat( ) * 10 - 5

Assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all. What does the following code do? Scanner scan = new Scanner(System.in); int value1 = scan.nextInt( ); int value2 = scan.nextInt( ); candy[value1] += value2;

adds value2 to the number of bars sold by child value1

If a and b are both int arrays, then a = b; will

create an alias

Consider a method defined with the header: public void foo(int a, int b). Which of the following method calls is legal?

foo(0 / 1, 2 * 3);

Assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all. Which of the following code could be used to compute the total number of bars sold by the children?

for (int j=0; j<12; j++) sum += candy[j];

Assume values is an int array that is currently filled to capacity, with the following values: 9 4 12 2 6 8 18 Which of the following loops would adequately add 1 to each element stored in the array values ?

for (int j=0; j<values.length; j++) values[j]++;

Consider the following class definition: import java.text.DecimalFormat; public class Student { private String name; private String major; private double gpa; private int hours; public Student(String newName, String newMajor, double newGPA, int newHours) { name = newName; major = newMajor; gpa = newGPA; hours = newHours; } public String toString( ) { DecimalFormat df = new DecimalFormat("0.000"); return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours; } } Another method that might be desired is one that updates the Student's number of credit hours. This method will receive a number of credit hours and add these to the Student's current hours. Which of the following methods would accomplish this?

public void updateHours(int moreHours) { hours += moreHours; }

Consider the following class definition: import java.text.DecimalFormat; public class Student { private String name; private String major; private double gpa; private int hours; public Student(String newName, String newMajor, double newGPA, int newHours) { name = newName; major = newMajor; gpa = newGPA; hours = newHours; } public String toString( ) { DecimalFormat df = new DecimalFormat("0.000"); return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours; } } Assume that another method has been defined as public String getClassRank( ) that will compute and return the student's class rank (Freshman, Sophomore, etc). Given that s1 is a student, which of the following would properly be used to get s1's class rank?

s1.getClassRank();

An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship?

this

Which of the following stores 78 into the last element of an int array x?

x[x.length-1] = 78;


Kaugnay na mga set ng pag-aaral

Geometry: Chapter 8 Review: Right Triangles and Trigonometry

View Set

16' BUSINESS MANAGEMENT+ ADMINISTRATION CAREER CLUSTER

View Set

Chapter 5: Roots > Bracketing Methods

View Set

Unit 4 - The Secondary Mortgage Market (Questions)

View Set

Chapter 8: Foreign Direct Investment

View Set

Pec major static stretch - daily routine 1

View Set

What makes people happy at work?

View Set