Exam 1 Review - INSY 4305

Ace your homework & exams now with Quizwiz!

Which of the following is not an error (either a syntax error or a logic error)? Neglecting to include an action in the body of a while statement that will eventually cause the condition to become false. Spelling a keyword (such as while or if) with a capitalized first letter. Using a condition for a while statement that is initially false. An infinite loop.

Using a condition for a while statement that is initially false.

All import declarations must be placed a. inside the class declaration's body. b. before the class declaration. c. after the class declaration. d. all of the above will work.

b

Consider the following Java statements: int x = 9; double y = 5.3; result = calculateValue(x, y); Which of the following statements is false? a. A method is called with its name and parentheses. b. x and y are parameters. c. Copies of x and y are passed to the method calculateValue. d. x and y are arguments.

b

Method calls cannot be distinguished by ________. a. method name b. return type c. parameter lists d. method signature

b

The format specifier ________ is a placeholder for an int value. a. %n b. %d c. %int d. %s

b

To declare a method as static, place the keyword static before ________ in the method's declaration. a. the method modifier b. the return type c. the method name d. the argument list

b

To exit out of a loop completely, and resume the flow of control at the next statement after the loop, use a _______. a. continue statement. b. break statement. c. return statement. d. Any of the above.

b

Which expression is equivalent to if (!(grade == sentinelValue))? a. if (grade !== sentinelValue) b. if (grade != sentinelValue) c. ! if (grade == sentinelValue) d. ! if (grade !== sentinelValue)

b

Which of the following for-loop headers results in equivalent numbers of iterations: a. for (int q = 1; q <= 100; q++) b. for (int q = 100; q >= 0; q--) c. for (int q = 99; q > 0; q -= 9) d. for (int q = 990; q > 0; q -= 90)

b

Which statement below is false? a. Structured programming produces programs that are easier to test. b. Structured programming requires four forms of control. c. Structured programming produces programs that are easier to modify d. Structured programming promotes simplicity.

b. Structured programming requires four forms of control. (Only three forms are necessary: sequence, selection, iteration)

Which of the following is the escape character? a. * b. \ c. \n d. "

b. \

Which command compiles the Java source code file Welcome.java? a. cd Welcome.java b. javac Welcome.java c. java Welcome.java d. compile Welcome.java

b. javac Welcome.java

A(n) ________ enables a program to read data from the user. a. printf. b. import declaration. c. Scanner. d. main.

c

Consider the following two Java code segments: Segment 1 int i = 0; while (i < 20) { i++; } System.out.println(i); } Segment 2 for (int i = 0; i <= 20; i++) { System.out.println(i); Which of the following statements are true? a. The output from these segments is not the same. b. The scope of the control variable i is different for the two segments. c. Both (a) and (b) are true. d. Neither (a) nor (b) is true.

c

Declaring main as static allows the JVM to invoke main ________. a. without knowing the name of the class in which main is declared. b. by creating an object of the class in which main is declared. c. without creating an instance of the class in which main is declared. d. None of the above.

c

Portions of statements that contain calculations are called a. variables. b. constants. c. expressions. d. None of the above.

c

When an object is concatenated with a String, ________. a. a compilation error occurs b. a runtime error occurs c. the object's toString method is implicitly called to obtain the String representation of the object d. the object's class name is concatenated with the String

c

Which is a correct static method call of Math class method sqrt? a. sqrt(900); b. math.sqrt(900); c. Math.sqrt(900); d. Math math = new Math();sqrt(900);

c

Which of the following code segments does not increment val by 3: a. val += 3; b. val = val + 1; val = val + 1; val = val + 1; c. c = 3;val = val + (c == 3 ? 2 : 3); d. All of the above increment val by 3.

c

Which of the following is a double-selection control statement? a. do...while b. for c. if...else d. if

c

Which of the following is not an algorithm? a. A recipe. b. Operating instructions. c. Textbook index. d. Shampoo instructions (lather, rinse, repeat).

c

Which of the following is not an arithmetic operator? a. + b. - c. . d. %

c

Which of the following methods is not in the Math class? a. ceil b. abs c. parseInt d. log

c

Which of the following statements about a do...while iteration statement is true? a. The body of a do...while loop is executed only if the terminating condition is true. b. The body of a do...while loop is executed only once. c. The body of a do...while loop is always executed at least once. d. None of the above.

c

Which of the following statements is true? a. Each object (instance) of the class shares the class's instance variables. b. Most instance-variable declarations are preceded with the keyword public, which is an access modifier. c. Variables or methods declared with access modifier private are accessible only to methods of the class in which they're declared. d. None of the above is true.

c

What is the value of z after the following code is executed? int x = 5, y = 28; float z; z = (float) (y / x); a. 5.6 b. 3.0 c. 5.0 d. 5.60

c. 5.0

Which edition of Java is geared toward developing large-scale, distributed networking applications and web-based applications? a. Standard Edition. b. Industrial Edition. c. Enterprise Edition. d. Micro Edition.

c. Enterprise Edition.

Which of the following does not contain a syntax error? a. System.out.println('Hello world!'): b. System.out.println("Hello world!"); c. System.out.println("Hello world!"); d. System.out.println(Hello world!);

c. System.out.println("Hello world!");

When method printf requires multiple arguments, the arguments are separated with ________. a. colons (:). b. semicolons (;). c. commas (,). d. periods (.).

c. commas (,).

Which command executes the Java class file Welcome.class? a. java welcome b. java Welcome.class c. java Welcome d. run Welcome.class

c. java Welcome

Java requires a ________ call for every object that's created.

constructor

A static method can ________. a. call only other static methods of the same class directly b. manipulate only static fields in the same class directly c. be called using the class name and a dot (.) d. All of the above.

d

A well-designed method ________. a. performs multiple unrelated tasks b. repeats code found in other methods c. contains thousands of lines of code d. performs a single, well-defined task

d

Any field declared with keyword ________ is constant. a. static b. const c. constant d. final

d

Consider the classes below: public class TestA { public static void main(String[] args) { int x = 2;int y = 20 int counter = 0; for (int j = y % x; j < 100; j += (y / x)) { counter++; } }} public class TestB { public static void main(String[] args) { int counter = 0; for (int j = 10; j > 0; --j) { ++counter;} } } Which of the following statements is true? a. The value of counter will be different at the end of each for loop for each class. b. The value of j will be the same for each loop for all iterations c. Both (a) and (b) are true. d. Neither (a) nor (b) is true.

d

Each class you create becomes a new ________ that can be used to declare variables and create objects. a. package b. instance c. library d. type.

d

Which of the following is not a control structure: Sequence structure. Selection structure. Iteration structure. Declaration structure.

Declaration structure.

Counter-controlled iteration is also known as:

Definite iteration

What does the expression x %= 10 do? Adds 10 to the value of x, and stores the result in x. Divides x by 10 and stores the remainder in x. Divides x by 10 and stores the integer result in x. None of the above.

Divides x by 10 and stores the remainder in x.

End-of-line comments that should be ignored by the compiler are denoted using

Two forward slashes (//).

Which of the following operators associates from left to right? = ?: %= /

/

How many times is the body of the loop below executed? int counter = 1; while (counter > 20) { // body of loop counter = counter - 1; }

0

What is the result value of c at the end of the following code segment? int c = 8; c++; ++c; c %= 5;

0

How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100);

1

Which of the following is not a Java keyword? do next while for

next

Which statement is true? Dividing two integers results in integer division. With integer division, any fractional part of the calculation is lost. With integer division, any fractional part of the calculation is truncated. All of the above.

All of the above.

Types in Java are divided into two categories. The primitive types are boolean, byte, char, short, int, long, float and double. All other types are ________ types.

reference

What is output by the following Java code segment? int temp = 200; if (temp > 90) { System.out.println("This porridge is too hot."); } if (temp < 70) { System.out.println("This porridge is too cold."); } if (temp == 80) { System.out.println("This porridge is just right!");}

This porridge is too hot.

boolean values can be displayed as the words true and false with the ________ format specifier.

%b.

The format specifier ________ is a placeholder for an int value.

%d

A Java class can have which of the following methods? A. void foo(int a) B. void foo(int a, int b) C. void foo(double a) D. void foo(double a, double b) E. void foo(int b)

A, B, C, D.

Which of the following statements is true? Local variables are automatically initialized. Every instance variable has a default initial value—a value provided by Java when you do not specify the instance variable's initial value. The default value for an instance variable of type String is void. The argument types in the method call must be identical to the types of the corresponding parameters in the method's declaration

Every instance variable has a default initial value—a value provided by Java when you do not specify the instance variable's initial value.

Sentinel-controlled iteration is also known as:

Indefinite iteration.

Java is considered a strongly typed language because: The primitive types in Java are portable across all computer platforms that support Java. Java requires all variables to have a type before they can be used in a program. Instance variables of the primitive types are automatically assigned a default value. All of the above.

Java requires all variables to have a type before they can be used in a program.

Which of the following statements is false? Variables declared in the body of a particular method are local variables and can be used only in that method. A method's parameters are local variables of the method. Every method's body is delimited by left and right braces ({ and }). Keyword null indicates that a method will perform a task but will not return any information.

Keyword null indicates that a method will perform a task but will not return any information. Actually, keyword void indicates that a method will perform a task but will not return any information.

Where can local variables declared within a method's body be used? Only in that method between the line in which they were declared and the closing brace of that method. Same as (a), but not within while or if statements. Only within while or if statements within the method in which they were declared. Anywhere within the class.

Only in that method between the line in which they were declared and the closing brace of that method.

Which of the following statements is false? In the UML, each class is modeled in a class diagram as a rectangle with three compartments. The top one contains the class's name centered horizontally in boldface. The middle one contains the class's attributes, which correspond to instance variables in Java. The bottom one contains the class's operations, which correspond to methods and constructors in Java. UML represents instance variables as an attribute name, followed by a colon and the type. Private attributes are preceded by the keyword private in the UML. The UML models operations by listing the operation name followed by a set of parentheses. A plus sign (+) in front of the operation name indicates that the operation is a public.

Private attributes are preceded by the keyword private in the UML. Actually, private attributes are preceded by a minus sign (-) in the UML.

Which of the following statements is false? A reference to an object is required to invoke an object's methods. A primitive-type variable does not refer to an object. Reference-type instance variables are initialized by default to the value void. A primitive-type variable cannot be used to invoke a method.

Reference-type instance variables are initialized by default to the value void. Actually, Reference-type instance variables are initialized by default to the value null.

The empty statement is denoted by what symbol?

Semicolon ;

Which of the following statements is true? Both syntax errors and logic errors are caught by the compiler. Both syntax errors and logic errors have effects at execution time. Syntax errors are caught by the compiler. Logic errors have effects at execution time. Logic errors are caught by the compiler. Syntax errors have effects at execution time.

Syntax errors are caught by the compiler. Logic errors have effects at execution time.

Which of the following statements about the break statement is false? The break statement is used to exit an iteration structure early and continue execution after the loop. A break statement can only break out of an immediately enclosing while, for, do...while or switch statement. The break statement, when executed in a while, for or do...while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch.

The break statement, when executed in a while, for or do...while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop.

Which of the following statements about the conditional operator (?:) is false? The conditional operator is a ternary operator, meaning that it takes three operands. The first operand is a boolean expression. The second operand is the result value if the condition evaluates to false. The second operand is the result value if the condition evaluates to true.

The second operand is the result value if the condition evaluates to false.

Overloaded methods always have the same _________. a. method name b. return type c. number of parameters d. order of the parameters

a

Suppose variable gender contains MALE and age equals 60, how is the expression (gender == FEMALE) && (age >= 65) evaluated? a. The condition (gender == FEMALE) is evaluated first and the evaluation stops immediately. b. The condition (age >= 65) is evaluated first and the evaluation stops immediately. c. Both conditions are evaluated, from left to right. d. Both conditions are evaluated, from right to left.

a

Which of the following is a variable declaration statement? a. int total; b. import java.util.Scanner; c. public static void main(String args[]) d. // first string entered by user

a

Which of the following is not a valid Java identifier? a. my Value b. $_AAA1 c. width d. m_x

a

Which of the following statements is false? a. Each class declaration that begins with the access modifier private must be stored in a file that has the same name as the class and ends with the .java filename extension. b. Every class declaration contains keyword class followed immediately by the class's name. c. Class, method and variable names are identifiers. d. An object has attributes that are implemented as instance variables and carried with it throughout its lifetime.

a

Which of the following statements is true? a. Strings can be used in a switch statement's controlling expression and in its case labels. b. Strings can be used in a switch statement's controlling expression but not in its case labels. c. Strings cannot be used in a switch statement's controlling expression but can be used in its case labels. d. Strings cannot be used in a switch statement's controlling expression and cannot be used in its case labels.

a

Which of the following will not help prevent infinite loops? a. Include braces around the statements in a do...while statement. b. Ensure that the header of a for or while statement is not followed by a semicolon. c. If the loop is counter-controlled, the body of the loop should increment or decrement the counter as needed. d. If the loop is sentinel-controlled, ensure that the sentinel value is input eventually.

a

What will be output after the following Java statements have been executed (assume all variables are of type int)? a = 4; b = 12; c = 37; d = 51; if (a < b) { System.out.println("a < b");} if (a > b) { System.out.println("a > b"); } if (d <= c) { System.out.println("d <= c"); } if (c != d) { System.out.println("c != d"); }

a < b c != d

Which is the output of the following statements? System.out.print("Hello "); System.out.println("World"); a. Hello World b. HelloWorld c. Hello World d. World Hello

a. Hello World

Which of the following statements would display the phase Java is fun? a. System.out.println("hello \rJava is fun"); b. System.out.println('Java is fun'); c. System.out.println("\"Java is fun\""); d. System.out.println(Java is fun);

a. System.out.println("hello \rJava is fun");

For the two code segments below: Segment A int q = 5; switch(q) { case 1: System.out.println(1); case 2: System.out.println(2); case 3: System.out.println(3); case 4: System.out.println(4); case 5: System.out.println(5); default: System.out.println("default"); } Segment B q = 4; switch(q) { case 1: System.out.println(1); case 2: System.out.println(2); case 3: System.out.println(3); case 4: System.out.println(4); case 5: System.out.println(5); default: System.out.println("default"); } Which of the following statements is true? a. The output for Segment A is: default b. The output for Segment B is: 4 c. The output for Segment B is: 45default d. The output for Segment A is: 5 default

d

In a class containing methods with the same name, the methods are distinguished by ________. a. Number of arguments b. Types of arguments c. Return type d. (a) and (b) e. (b) and (c)

d

Information is passed to a method in ________. a. the method name b. that method's return c. the method body d. the arguments to the method

d

Java's predefined classes are grouped into a. packets. b. declarations. c. Galleries. d. packages.

d

The parameter list in the method header and the arguments in the method call must agree in: a. number b. type c. order d. all of the above

d

Which of the following can be an argument to a method? a. Constants. b. Variables. c. Expressions. d. All of the above.

d

Which of the following is true? a. Pseudocode is used to describe an algorithm. b. Pseudocode is not an actual computer c. programming language. c. Pseudocode is used to describe executable statements that will eventually be translated by the programmer into a program. d. All of the above.

d

Which of the following statements about the continue statement is true? a. The continue statement is used to exit an iteration structure early and continue execution after the loop. b. The continue statement is used to continue after a switch statement. c. The continue statement does not alter the flow of control. d. A continue statement proceeds with the next iteration of the immediately enclosing while, for, do...while statement.

d

Which of the following statements is true? a. System.out.print("Enter your age: "); prompts the user to take action. b. Class names typically begin with a capital letter. c. Package java.lang is imported in every Java program. d. All of the above are true.

d

Which of the following will count down from 10 to 1 correctly? a. for (int j = 10; j <= 1; j++) b. for (int j = 1; j <= 10; j++) c. for (int j = 10; j > 1; j--) d. for (int j = 10; j >= 1; j--)

d

Which of the following cannot cause a syntax error to be reported by the Java compiler? a. Mismatched {} b. Missing */ in a comment that begins with /* c. Missing ; d. An extra blank line.

d. An extra blank line.

Which of the following statements is false? a. Object-oriented programming is today's key programming methodology. b. Java has become the language of choice for implementing Internet-based applications and software for devices that communicate over a network. c. Software commands computer hardware to perform tasks. d. In use today are more than a trillion general-purpose computers and trillions more Java-enabled cellphones, smartphones and other handheld devices.

d. In use today are more than a trillion general-purpose computers and trillions more Java-enabled cellphones, smartphones and other handheld devices.

What would be displayed as a result of executing the following code? final int x = 22, y = 4; y += x; System.out.println("x = " + x + ", y = " + y) a. x = 22, y = 26 c. x = 22, y = 88 b. x = 22, y = 4 d. Nothing. There is an error in the code.

d. Nothing. There is an error in the code.

Programs remember numbers and other data in the computer's memory and access that data through program elements called a. comments. b. messages. c. integers. d. variables.

d. variables.

When a method terminates, the values of its local variables are ________.

lost

Which primitive type can hold the largest value? int long float double

double

A class that creates an object of another class, then calls the object's methods, is called a(n) ________ class.

driver

Consider the code segment below. if (gender == 1) { if (age >= 65) { ++seniorFemales;} } This segment is equivalent to which of the following? if (gender == 1 || age >= 65) {++seniorFemales; } if (gender == 1 && age >= 65) { ++seniorFemales; } if (gender == 1 AND age >= 65) { ++seniorFemales; } if (gender == 1 OR age >= 65) { ++seniorFemales; }

if (gender == 1 && age >= 65) { ++seniorFemales; }

You must call most methods other than ________ explicitly to tell them to perform their tasks.

main

Which of the following terms is not used to refer to a sentinel value that breaks out of a while loop? signal value. maximum value. dummy value. flag value.

maximum value.

Local variables must be ________. initialized when they're declared. initialized before their values are used in an expression. declared and initialized in two steps. declared at the top of the method's body.

initialized before their values are used in an expression.

The control variable of a counter-controlled loop should be declared as ________to prevent errors.

int

Which of the following segments is a proper way to call the method readData four times? double k = 0.0; while (k != 4) { readData(); k = k + 1; } int i = 0; while (i <= 4) { readData(); i = i + 1; } int i = 0; while (i < 4) { readData(); i = i + 1;}

int i = 0; while (i < 4) { readData(); i = i + 1;}

Which of the following is equivalent to this code segment? int total = 0; for (int i = 0; i <= 20; i += 2) { total += i; }

int total = 0;for (int i = 0; i <= 20; total += i, i += 2) {}

In an expression containing values of the types int and double, the ________ values are ________ to ________ values for use in the expression.

int, promoted, double.

Declaring instance variables ________ is known as data hiding or information hiding.

private

A key part of enabling the JVM to locate and call method main to begin the app's execution is the ________ keyword, which indicates that main can be called without first creating an object of the class in which the method is declared.

static

If a class does not define constructors, the compiler provides a default constructor with no parameters, and the class's instance variables are initialized to ________.

their default values


Related study sets

Lewin's Force Field Analysis (Organizational change)

View Set