EGR 281 Exam 3

Ace your homework & exams now with Quizwiz!

int[] num = new int[100]; for(int i = 0; i < 50; i++) num[i] = i; num[5] = 10; num[55] = 100; How many components are in the array above? A) 0 B) 50 C) 99 D) 100

100

What is the value of alpha[3] after the following code executes int[] alpha = new int[5]; int j; alpha[0] = 5; for (j = 1; j < 5; j++) { if (j % 2 == 0 ) alpha[j] = alpha[ j - 1] + 2; else alpha[j] = alpha[ j - 1] + 3; A) 10 B) 13 C) 15 D) None of these

15

String sentence; String str1, str2, str3; int length1, length2; sentence = "Today is Wednesday."; str1 = sentence.substring(9, 18); str2 = str1.substring(0, 3); str3 = sentence.replace('d', '*'); length1 = sentence.length(); length2 = str1.length(); Based on the code above what is the value of length1? a) 16 b) 17 c) 19 d) 20

19

what is the value of alpha[2] after the following code executes int[] alpha = new int[5]; int j; for(j = 0; j < 5; j++) alpha[j] = 2 * j + 1; A) 1 B) 4 C) 5 D) 6

5

Consider the following program // Insertion Point 1 public class CircleArea { // Insertion Point 2 static final float PI = 3.14; public static void main(String[]args){ //Insertion Point 3 float r = 2.0; float area; area = PI * r * r; System.out.println("Area = " + area); } //insertion point 4 } In the above code, where do the import statements belong? A) Insertion Point 1 B) Insertion Point 2 c) Insertion Point 3 D) Insertion Point 4

Insertion Point 1

Which statement best describes overriding a method? A) Methods in a base class and derived class have the same name but different visibility modifiers. B) Methods in a base class and derived class have the same name and have the same number and types of parameters. C) Methods in a base class and derived class have the same name but have different return types. D) Methods in a base class and derived class have the same name but different number or types of parameters.

Methods in a base class and derived class have the same name and have the same number and types of parameters.

Suppose alpha is an array of 50 components. Which of the following about alpha is true? (i) The base address of alpha is the address of alpha[0]. (ii) The base address of alpha is the address of alpha[1]. A) Only (i) B) Only (ii) C) Both (i) and (ii) D) None of these

Only (i)

Suppose you have the following declaration. int[] beta = new int[50]; Which of the following is a valid element of beta. (i) beta[0] (ii) beta [50] A) Only (i) B) Only (ii) C) Both (i) and (ii) D) None of these

Only (i)

Which of the following creates an array of 25 components of the type int? (i) int[] alpha = new[25]; (ii) int[] alpha = new int[25]; A) Only (i) B) Only (ii) C) Both (i) and (ii) D) None of these

Only (ii)

int[] num = new int[100]; for(int i = 0; i < 50; i++) num[i] = i; num[5] = 10; num[55] = 100; what is the value at index 10 in the array above a)0 b)5 c)10 d)50

10

The parselnt method is in the ____ class A) Integer B) int C) Parser D) Input

Integer

the method used to convert from Character to char is ___

charValue()

the method ____ in the Math class returns the nearest whole number that is equal to or less than its argument A) abs B) min C) round D) floor

floor

consider the following declaration double[] sales = new double [50]; int j; Which of the following correctly initializes all components of the array sales to 10. (i) for (j = 0; j < 49; j++) sales[j] = 10; (ii) for (j = 1; j <=50; j++) sales[j] = 10: A) only (i) B) only (ii) C) both (i) and (ii) D) none of these

only (i)

suppose you have the following declaration. double[] salesData = new double[1000]; which of the following range is valid for the index of the array salesData (i) 0 through 999 (ii) 1 through 1000 a) only (i) b) only (ii) c) both are invalid d)both are valid

only (i)

Consider the following declaration. int[] list = new int [10]; int j; Which of the following correctly outputs all the elements of list. (i) for(j = 1; j < 10; j++) System.out.print(list[j] + " "); System.out.println(); (ii) for(j = 1; j <= 9; j++) System.out.print(list[j] + " "); System.out.println(); a) only (i) b) only (ii) c) both (i) and (ii) d) none of these

only (ii)

given the declaration int[] gamma = new int[50]; int j; Which of the following for loops set the index of gamma out of bounds? (i) for (j = 0; j <=49; j++) System.out.print(gamma[j] + " "); (ii) for (j = 0; j <50; j++) System.out.print(gamma[j] + " "); (iii) for (j = 0; j <=50; j++) System.out.print(gamma[j] + " "); a) only (i) b) only (ii) c) only (iii) d) all (i), (ii), (iii)

only (iii)

___ means that two or more methods can have different names in the same way that an English word can have two or more meanings. A) object-oriented B) encapsulation C) polymorphism D) inheritance

polymorphism

Normally all instance variables are given the qualifier A) public B) protected C) private D) static

private

Assume stateArr is an array of String objects containing the names of the 50 US states in alphabetical order. Select the Java statement that is used in a selection statement to determine whether "Montana" is the state at index 25 (stateArr[25]) if (___________) System.out.println("Montana is the state at index 25") A) stateArr(25] == "Montana" B) ((String)stateArr)[25].equals("Montana") C) (String)(stateArr[25] == "Montana") D) stateArr[25].equals("Montana")

stateArr[25].equals("Montana")

a ___ method is a method that can be invoked using the class name instead of an object name.

static

according to the figure above (str points to address 2500 and address 2500 belongs to the data Java Programming) str contains A) the value 2500 B) the value Java Programming C) the address 2500 D) None of these

the address 2500

What determines whether a method definition in a derived class of an ancestor class will be used? A) the return type of the method B) the type of the variable that names the object C) the object's place in the inheritance chain D) the derived class's descendants

the object's place in the inheritance chain

A __ method does not return a value A) null B) static C) void D) recursive

void

One kind of method returns a value. The other kind of method is called a(n) ___

void

The name of a constructor A) can be any legal identifier B) must include the name of the package C) will always be the word new D) will always be the same as the name of the class

will always be the same as the name of the class

a ___ allows you to have a class object that corresponds to a value of a primitive type. A) wrapper class B) type cast C) constructor D) static variable

wrapper class

Which of the following declares an array of int named hits? A) int hits; B) int[] hits; c) new int hits[]; D) int hits = int[];

int[] hits;

int [] x = new int[10]; x[0] = 34; x[1] = 88; System.out.println(x[0] + " " + x[1] + " " + x[10]); what is the output of the code fragment above A) 34 88 0 B) 34 88 88 C) 0 34 88 D) this program throws an exception

0 34 88

What is the output of the following java code? int[] list = {0, 5, 10, 15, 20}; int j; for (j =0; j < 5; j++) System.out.print(list[j] + " "); System.out.println(); A) 0 1 2 3 4 B) 0 5 10 15 20 C) 0, 5, 10, 15, 20 D) None of these

0 5 10 15 20

int[] hit = new hit[5]; hit[0] = 3; hit[1] = 5; hit[2] = 2; hit[3] = 6; hit[4] = 1; System.out.println(hit[1 + 3]); What is the output of the code fragment above? A) 1 B) 3 C) 5 D) 6

1

what is stored in alpha after the following code executes? int[] alpha = new int[5]; int j; for (j = 0; j < 5; j++) { alpha[j] = 2 * j; if (j % 2 == 1) alpha[j -1] = alpha[j] + j } A) alpha = {0, 2, 4, 6, 8} B) alpha = {3, 2, 9, 6, 8} C) alpha = {0, 3, 4, 7, 8} D) alpha = {0, 2, 9, 6, 8}

alpha = {0, 3, 4, 7, 8}

what is stored in an alpha after the following code executes int[] alpha = new int[5]; int j; for (j = 0; j < 5; j++) { alpha[j] = j + 1; if( j > 2) alpha[j - 1] = alpha[j] + 2; } A) alpha = {1, 5, 6, 7, 5} B) alpha = {1, 2, 3, 4, 5} C) alpha = {4, 5, 6, 7, 9} D) None of these

alpha = {1, 2, 3, 4, 5}

what is stored in alpha after the following code executes? int[] alpha = new int[5]; int j; for (j = 0; j < 5; j++) { alpha[j] = j + 5; if (j % 2 == 1) alpha[j -1] = alpha[j] + 2; } A) alpha = {5, 6, 7, 8, 9} B) alpha = {5, 6, 10, 8, 9} C) alpha = {8, 6, 7, 8, 9} D) alpha = {8, 6, 10, 8, 9}

alpha = {8, 6, 10, 8, 9}

how many constructors can a class have A) 0 B) 1 C) 2 D) any number

any number

consider the following declaration int[] list = new int[10]; int j; int sum; Which of the following correctly finds the sum of the elements of list? (i) sum = 0; for (j = 0; j < 10; j++) sum = sum + list[j]; (ii) sum = list[0]; for (j = 1; j < 10; j++) sum = sum + list[j]; A) only (i) B) only (ii) C) both (i) and (ii) D) none of these

both (i) and (ii)

A private instance variable A) can never be modified by a method outside the class B) can be modified by a method outside the class if it is returned by a method C) can be modified by a method outside the class if it is a class type, using a mutator method (assuming one exists) D) can be modified by using an object name followed by a dot and then the name of the instance variable

can be modified by using an object name followed by a dot and then the name of the instance variable

What is the output of the following java code? int[] list = {0, 5, 10, 15, 20}; int j; for (j =0; j <= 5; j++) System.out.print(list[j] + " "); System.out.println(); a) 0 5 10 15 20 b) 5 10 15 20 0 c) 5 10 15 20 20 d) code contains index out-of-bound

code contains index out-of-bound

A(n) ___ is a special kind of method used to initialize objects

constructor

A constructor with no parameters is called a ___ constructor

default

When comparing two objects to see if they have the same contents. one should A) use the = operator B) use the == operator C) define an equals method in the class and use it D) use the equals method to see if they have the same variable name

define an equals method in the class and use it

int larger(int x, int y) Given the method heading above, which of the following would be an incorrect demonstration of method overloading? a) int larger(int a, int b) b) int larger(int x, int y, int z) c) double larger(double x, double y) D) char larger(char x, char y, char z)

double larger(double x, double y)

Which of the following is a reserved word in Java? A) extends B) pane C) event D) inheritance

extends

Selection sort works by repeatedly A) using the position of elements to split the list into two sublists of equal size B) finding the smallest element in the remainder of the list and moving it forward C) comparing adjacent elements in the list and swapping them if they are out of order D) using a threshold value to split the list into two sublists where one sublist's elements are greater than the threshold and the other sublist's elements are less than the threshold

finding the smallest element in the remainder of the list and moving it forward

Java will automatically define a constructor A) if the programmer does not define a default constructor for a class B) if the programmer does not define any constructors for a class C) if the program refers to a constructor with no parameters D) for every class defined in a program

if the programmer does not define any constructors for a class

int[] num = new int[100]; for(int i = 0; i < 50; i++) num[i] = i; num[5] = 10; num[55] = 100; what is the data type in the array above? a)int b)char c)list d)num

int

int larger(int x, int y) Given the method heading above, which of the following does NOT demonstrate method overloading? A) int larger(int x, int y, int z) B) int larger(char x) C) int max(int x, int y) D) double larger(double x, double y)

int max(int x, int y)

public static int minimum(int x, int y) { int smaller; if (x < y) smaller = x; else smaller = y; return smaller; } Which of the following is NOT part of the heading of the method above? A) public B) static C) minimum(int x, int y) D) int smaller;

int smaller;

Which of the following is correct Java code? A) int[10] list; B) int list[10]; C) int list = new int[10]; D) int[] list = new int[10];

int[] list = new int[10];

The expression super.foo()in a method A) is a call to a method named super.foo in the same class B) is a call to the foo method in the base class C) is a call to the constructor of the class named foo D) is a syntax error

is a call to the foo method in the base class

The highest index that can be used with an array A) is the number of elements in the array B) is the number of elements in the array minus one C) is determined by the array declaration D) changes as the program runs

is the number of elements in the array minus one

every array object has an instance variable called ___ that tells how many elements are in the array

length

A variable declared inside a method is called a ___ variable

local

A ___ is an action that an object can take and is specified in the class definition.

method

According to the UML class diagram above, which of the following is a data member? A) Clock() B) printTime() C) min D) Clock

min

public static int minimum(int x, int y) { int smaller; if (x < y) smaller = x; else smaller = y; return smaller; } what is the name of the method above A) x B) y C) smaller D) minimum

minimum

public static int minimum(int x, int y){ int smaller; if (x < y) smaller - x; else smaller - y; return smaller; } Which of the following is a valid call to the method above A)minimum(5, 4); B) minimum(int 5, int 4); C)minimum(int x, int y); D)public static int minimum(5, 4);

minimum(5, 4);

Which of the following method is contained in the package java.util? A) format B) showMessageDialog C) nextInt D) printf

nextInt

___ is a special constant that is not an object but can be used to give a value to any variable of class type. a)void b)this c) null D) false

null

In the description of a method, a ___ specifies a necessary condition that must apply in order that the operation can execute successfully.

precondition

Which modifier is used to specify that a method cannot be used outside a class? A) public B) abstract C) static D) private

private

in a class the modifier ___ identifies data and operations that can be used only by methods in the class

private

According to the UML class diagram above, which of the following would be a default constructor for the class Clock shown in the figure above? A) public Clock(){setTime(0, 0, 0); } B) public Clock(0, 0, 0) { setTime(0, 0, 0); } C) public Clock(0) { setTime(0); } D) private Clock(10){ setTime(10); }

public Clock(){setTime(0, 0, 0); }

int[] hits = {10, 15, 20, 25, 30} copy(hits); What is being passed into copy in the method call above? A) A copy of the array hits B) The value of the elements of hits C) A reference to the array object hits D) 10

A reference to the array object hits

What will most probably happen if you omit the dot operator when accessing a method? A) An exception will be thrown B) An exception may be thrown. C) A syntax error will be reported. D) The program will execute as planned.

A syntax error will be reported.

Consider the following declaration. int[] beta = new int[3]; int j; Which of the following input statements correctly input values into beta? (Assume that console is a Scanner object initialized to the standard input device) (i) beta[0] = console.nextInt(); beta[1] = console.nextInt(); beta[2] = console.nextInt(); (ii) for (j = 0; j < 3; j++) beta[j] = console.nextInt(); A) Only (i) B) Only (ii) C) Both (i) and (ii) D) None of these

Both (i) and (ii)

How can a method send a primitive value back to the caller? A) It cannot send primitive values back to the caller B) By using the return statement C) By assigning the value to one of its parameters D) It can call its caller with the value

By using the return statement

Which of the following is not a basic array operation? A) Deleting an element B) Inserting an element C) Replacing an element D) Changing an element's class type

Changing an element's class type

Write a program that takes as input the radius of a circle and finds the circumference and area of the circle. Based on the problem statement above, which of the following would most likely be chosen as the class? A) Circumference B) Circle C) Area D) Radius

Circle

Which of these words is the name of a wrapper class? A) int B) float C) Double D) String

Double

All data stored in an array must be the same type. (A) True (B) False

False

Every array has a method called equals that can only be used to compare it to other arrays. A) True B) False

False

If one element from an array is passed as a parameter to a method, then the called method can modify any element in the array. A) True B) False

False

It is possible to have two methods in the same class that have the same name, the same number and types of arguments but different return types. A) True B) False

False

The size of an array in java is determined when the program is compiled A) True B) False

False

You can change the size of an array by assigning a new value to the length instance variable. A) True B) False

False

the method used to convert from String to Float is ___

Float.parseFloat(String)

Which statement best describes an array of 10 elements where the elements are of class String? A) 10 String objects will be created when the array is created. B) 10 String objects must be created before the array is created. C) The array will be created with the elements equal to null. D) Elements of an array cannot be a class type.

The array will be created with the elements equal to null.

String sentence; String str1, str2, str3; int length1, length2; sentence = "Today is Wednesday."; str1 = sentence.substring(9, 18); str2 = str1.substring(0, 3); str3 = sentence.replace('d', '*'); length1 = sentence.length(); length2 = str1.length(); Based on the code above, what is the value of str1 A) Today B) Wednesda C) Wednesday D) Wednesday.

Wednesday

public static int minimum(int x, int y) { int smaller; if (x < y) smaller = x; else smaller = y; return sender; } Based on the above code what would be the value stored in the variable s? int s = minimum(5, minimum(3, 7)); A) 3 B) 5 C) 7 D) There would be no output: this is not a valid statement

There would be no output: this is not a valid statement

String sentence; String str1, str2, str3; int length1, length2; sentence = "Today is Wednesday."; str1 = sentence.substring(9, 18); str2 = str1.substring(0, 3); str3 = sentence.replace('d', '*'); length1 = sentence.length(); length2 = str1.length(); Based on the code above, what is the value of str3 A) Today is Wednesday. B) To*ay is We*nes*ay. C) Today * Wednesday. D) d** ** **d***d*

To*ay is We*nes*ay.

String sentence; String str1, str2, str3; int length1, length2; sentence = "Today is Wednesday."; str1 = sentence.substring(9, 18); str2 = str1.substring(0, 3); str3 = sentence.replace('d', '*'); length1 = sentence.length(); length2 = str1.length(); Based on the code above, what is the value of str2 A) Tod B) Wed C) Wedn D) is

Tod

An accessor method that returns an array should return a copy of an instance variable array rather than the instance variable itself. A) True B) False

True

An equals method should return a boolean value. A) True B) False

True

Arrays in Java are objects A) True B) False

True

If you add at least one constructor to a class, no constructors will be automatically created for the class. A) True B) False

True

Parameters of a primitive type are passed to methods using the call-by-value mechanism A) True B) False

True

The index to use with an array can be an expression whose value is computed when the program runs A) True B) False

True

When a method call is executed, values from the method definition are substituted for the arguments in the method call. A) True B) False

True

Write a program that takes as input the radius of a circle and finds the circumference and area of the circle. Based on the problem statement above, which of the following would be a data member? A) radius B) area C) circumference D) None of the above

radius

Which of the following is NOT a modifier? A) public B) static C) return D) protected

return

Which of the following is NOT true about return statements? A) A value-returning method returns its value via the return statement. B) return statements can be used in void methods to return values. C) A method can have more than one return statement. D) Whenever a return statement executes in a method, the remaining statements are skipped and the method exits

return statements can be used in void methods to return values.

names of mutator methods usually begin with ___

set

According to the UML class diagram above, which method is public and doesn't return anything? A) getCopy() B) setTime(int, int, int) C) equals(Clock) D) incrementHours()

setTime(int, int, int)

A method is invoked by A) writing the name of the method followed by a colon and the name of the calling object B) writing the name of the calling object followed by a dot and a list of values in parentheses C) listing the name of the calling object and the name of the method inside parentheses D) writing the name of the calling object, followed by a dot, followed by the name of the method and a list of values in parentheses

writing the name of the calling object, followed by a dot, followed by the name of the method and a list of values in parentheses


Related study sets

TJC US History 1302 Comprehensive Final Exam

View Set

Chapter 11 - Shock, Sepsis, and Multiple Organ Dysfunction Syndrome

View Set

Photosynthesis and Cell Respiration

View Set

Evolve Maternity and Women's Health Nursing - Pregnancy, Uncomplicated

View Set

Mental Health Nursing Chapter 14 Depressive Disorders

View Set