Intro to Scientific Programming-Java
Another term for an object of a class is a(n)
instance
Methods that operate on an object's fields are called
instance methods
What will be the value of z as a result of executing the following code? int x = 5, y = 28; float z; z = (float) (y / x);
5.0
What is the result of the following expression? 17 % 3 * 2 - 12 + 15
7
The ________ statement is used to create a decision structure, which allows a program to have more than one path of execution. A) block B) if C) null D) flag
B
The boolean expression in an if statement must evaluate to A) degrees or radians. B) true or false. C) positive or negative. D) left or right.
B
This type of loop will always be executed at least once. A) pretest B) posttest C) infinite D) conditional
B
What will be the result of executing the following code? int[] x = {0, 1, 2, 3, 4, 5}; A) An array of 6 values, all initialized to 0 and referenced by the variable x, will be created. B) An array of 6 values, ranging from 0 through 5 and referenced by the variable x, will be created. C) The variable x will contain the values 0 through 5. D) A compiler error will occur.
B
What will be the result of the following code? final int ARRAY_SIZE = 5; float[] x = float[ARRAY_SIZE]; for(int i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; } A) A runtime error will occur. B) All the values in the array will be initialized to 10.0. C) All the values in the array, except the first, will be set to 10.0. D) The code contains a syntax error and will not compile.
B
What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; y += 20; } A) 130 B) 210 C) 110 D) 90
B
For the following code, which statement is not true? public class Sphere { private double radius; public double x; private double y; private double z; } A) The z field is available to code that is written outside the Sphere class. B) The radius field is not available to code written outside the Sphere class. C) The radius, x, y, and z fields are members of the Sphere class. D) The x field is available to code that is written outside the Sphere class.
A
In order to do a binary search on an array, A) the array must first be sorted in ascending order. B) you must first do a sequential search of the array to assure the element you are looking for is there. C) the values of the array must be numeric. D) All of the above
A
Java requires that the boolean expression being tested by an if statement be enclosed in A) a set of parentheses. B) a set of braces. C) a set of double quotes. D) a set of brackets.
A
What will be the value of x after the following code is executed? int x = 10; while (x < 100) { x += 10; } A) 100 B) 90 C) 110 D) 10
A
What will be the values of ans, x, and y after the following statements are executed? int ans = 35, x = 50, y = 50; if (x >= y) { ans = x + 10; x -=y; } else { ans = y + 10; y += x; } A) ans = 60, x = 0, y = 50 B) ans = 45, x = 50, y = 0 C) ans = 45, x = 50, y = 50 D) ans = 60, x = 50, y =100
A
Which of the following expressions will determine whether x is less than or equal to y? A) x <= y B) x => y C) x >= y D) x =< y
A
Which of the following statements determines whether temp is within the range of 0 through 100? A) if (temp >= 0 && temp <= 100) B) if (temp > 0 && temp < 100) C) if (temp >= 0 || temp <= 100) D) if (temp > 0 || temp < 100)
A
A constructor A) always accepts two arguments. B) has the same name as the class. C) has return type of void. D) always has an access specifier of private.
B
How many times will the following for loop be executed? for (int count = 10; count <= 21; count++) System.out.println("Java is great!"); A) 0 B) 12 C) 10 D) 11
B
In all but rare cases, loops must contain within themselves A) nested loops. B) a way to terminate. C) arithmetic operators. D) if statements.
B
In an if-else statement, if the boolean expression is false A) no statements or blocks are executed. B) the statement or block following the else is executed. C) the first statement or block is executed. D) all statements or blocks are executed.
B
Which of the following is not a rule that must be followed when naming identifiers? A) After the first character, you may use the letters a-z, A-Z, an underscore, a dollar sign, or digits 0-9. B) Identifiers can contain spaces. C) Uppercase and lowercase characters are distinct. D) The first character must be one of the letters a-z, A-Z, an underscore or a dollar sign.
B
A class that is defined inside of another class is called a(n) A) nested class. B) enumerated class. C) inner class. D) helper class.
C
A class's responsibilities include A) the things a class is responsible for knowing. B) the things a class is responsible for doing. C) both A and B D) neither A nor B
C
Assume the class BankAccount has been created, and the following statement correctly creates an instance of the class: BankAccount account = new BankAccount(5000.0); What is true about the following statement? System.out.println(account); A) A runtime error will occur. B) The method will display unreadable binary data on the screen. C) The account object's toString method will be implicitly called. D) A compiler error will occur.
C
Overloading is A) writing a method that does too much processing. B) writing a program that is too large to fit in memory. C) having two or more methods with the same name, but different signatures. D) having two or more methods with the same signature.
C
The ________ loop is ideal in situations where you always want the loop to iterate at least once. A) for B) while C) do-while D) pretest
C
The keyword new A) creates a new class. B) creates a new Java byte code file. C) creates an object in memory. D) creates a new variable in memory.
C
What is the value of scores[2][3] in the following array? int[][] scores = { {88, 80, 79, 92}, {75, 84, 93, 80}, {98, 95, 92, 94}, {91, 84, 88, 96} }; A) 95 B) 84 C) 94 D) 93
C
What will be the result of the following code? int[] numbers = {40, 3, 5, 7, 8, 12, 10}; int value = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] < value) value = numbers[i]; } A) The value variable will contain the average of all the values in the numbers array. B) The value variable will contain the sum of all the values in the numbers array. C) The value variable will contain the lowest value in the numbers array. D) The value variable will contain the highest value in the numbers array.
C
What will be the value of x after the following code is executed? int x = 75; int y = 60; if (x > y) x = x - y; A) 60 B) 75 C) 15 D) 135
C
The variable used to keep the running total is called a(n) A) sentinel. B) integer. C) summation. D) accumulator.
D
What will be the value of x after the following code is executed? int x = 10; for (int y = 5; y < 20; y +=5) x += y; A) 25 B) 30 C) 50 D) 40
D
What will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++; A) x = 34, y = 9 B) x = 25, y = 8 C) x = 33, y = 8 D) x = 33, y = 9
D
What would be the value of discountRate after the following statements are executed? double discountRate; char custType = 'B'; switch (custType) { case 'A': discountRate = 0.08; break; case 'B': discountRate = 0.06; case 'C': discountRate = 0.04; default: discountRate = 0.0; } A) 0.08 B) 0.06 C) 0.04 D) 0.0
D
Which of the statements are true about the following code? final int ARRAY_SIZE = 10; long[] array1 = new long[ARRAY_SIZE]; A) declares array1 to be a reference to an array of long values B) creates an instance of an array of 10 long values C) will allow valid subscripts in the range of 0 through 9 D) All of the above
D
This is a value that is written into the code of a program
Literal
________ is a cross between human language and a programming language.
Pseudocode
A local variable's scope always ends at the closing brace of the block of code in which it is declared. Answer:
True
If the expression on the left side of the && operator is false, the expression on the right side will not be checked. Answer:
True
Variables are classified according to their
data type
In a for loop, the control variable can only be incremented. Answer:
false
The for loop is a posttest loop that has built-in expressions for initializing, testing, and updating. Answer:
false
When testing for character values, the switch statement does not test for the case of the character. Answer:
false
Which Scanner class method reads a String?
nextLine
The two primary methods of programming in use today are
procedural and object-oriented
One of the design tools used by programmers when creating a model of the program is
pseudocode
The do-while loop is ideal in situations where you always want the loop to iterate at least once. Answer:
true
enum constants have a toString method. Answer:
true