COP2258 Final

Ace your homework & exams now with Quizwiz!

Quiz 4: Elementary Programming

...

Quiz 5: Selection

...

Quiz 6: Mathematical Functions, Characters, and Strings

...

Quiz 7: Loops

...

Quiz 8: Methods & Objects

...

What is the output of the following Java code? int num = 15; while (num > 0) num = num - 3; System.out.println(num); A. 0 B. 3 C. 12 D. 15

A. 0

What is i printed? public class Test { public static void main(String[] args) { int j = 1; int i = ++j + j * 5; System.out.println("What is i? " + i); } } A. 12 B. 8 C. 10 D. 6

A. 12

If the String variable str contains the string "Now is the time", what is the result of the folllowing epression? String str = "Now is the time"; System.out.println(str.length() + " " + str.substring(1,3)); A. 15 ow B. 10 Now C. 9 Now is D. 12 Now

A. 15 ow

What is x after the following code? public class Test { public static void main(String[] args) { int x = 3; x *= x + 5; System.out.println("x is " + x); } } A.24 B.20 C. 15 D. 18

A. 24

Suppose that s1, s2, and s3 are three strings, given as follows: String s1 ="Tallahassee, FL"; String s2 = "Atlanta, GA"; String s3 = "Birmingham, AL"; What is the results of the following expressions? s2.substring(3).lastIndexOf('a') A. 3 B. 0 C. 6 D. anta, GA E. GA F. ta, GA

A. 3

What is Math.ceil(3.6)? A. 4.0 B. 3.0 C. 5.0 D. 3

A. 4.0

What is the output of the following code? int count; int num = 2; for (count = 1; count < 2; count++) { num = num + 3; System.out.print(num + " "); } System.out.println(); A. 5 B. 5 8 C. 2 5 8 D. 5 8 11

A. 5

Which of the following loops prints "Welcome to Java" 10 times? A: for (int count = 1; count <= 10; count++) { System.out.println("Welcome to Java"); } B: for (int count = 0; count < 10; count++) { System.out.println("Welcome to Java"); } C: for (int count = 1; count < 10; count++) { System.out.println("Welcome to Java"); } D: for (int count = 0; count <= 10; count++) { System.out.println("Welcome to Java"); } A. AB B. ABC C. BD D. BC

A. AB

Suppose that s1, s2, and s3 are three strings, given as follows: String s1 ="Welcome to Java"; String s2 = "Programming is fun"; String s3 = "Welcome to Java"; What is the results of the following expressions? s2.startsWith("Wel") A. False B. 8 C. True D. 15

A. False

What is the output of System.out.println("Happy".substring(0,3))? A. Hap B. Happy C. Happ D. ppy

A. Hap

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

A. Insertion Point 1

What is the code that will display the middle initial 'L' (right justified) with a total of 10 spaces, given the following declaration? String middleName = "Lanette"; A. System.out.printf("Here is the first character of the middle name: %10c", middleName.charAt(0)); B. System.out.printf("Here is the first character of the middle name: %10s", middleName.charAt(1)); C. System.out.printf("Here is the first character of the middle name: %8f", middleName.indexAt(1)); D. System.out.printf("Here is the first character of the middle name: %10c", middleName.indexAt(0));

A. System.out.printf("Here is the first character of the middle name: %10c", middleName.charAt(0));

A(n) ____ is a program module that contains a series of statements that carry out a task. A. method B. argument C. application D. declaration

A. method

Assume x=4 and y=5, which of the following is true? A. x != 4 ^ y == 5 B. x != 5 ^ y != 4 C. x == 5 ^ y == 4 D. !(x == 4) ^ y != 5

A. x != 4 ^ y == 5

Given: char ch; int num; double pay; Which of the following assignment statements are valid? (i) ch = '*'; (ii) pay = num * pay; (iii) rate * 40 = pay; A. Only (i) is valid B. (i) and (ii) are valid C. (ii) and (iii) are valid D. (i) and (iii) are valid

B. (i) and (ii) are valid

What is the result of the following program? public class FindMax { public static void main(String[] args) { int max = 0; max(1,2,max); System.out.println(max); } public static void max(int value1, int value2, int max) { if (value1 > value2) max = value1; else max = value2; } } A. 1 B. 0 C. 3 D. 2

B. 0

The following loop displays _______________. for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++; } A. 1 2 3 4 5 B. 1 3 5 7 9 C. 1 2 3 4 5 6 7 8 9 D. 2 4 6 8 10

B. 1 3 5 7 9

What is Math.rint(3.6)? A. 3.0 B. 4.0 C. 3 D. 5.0

B. 4.0

What is the output of System.out.print( ("ABCDABC").lastIndexOf("BC") )? A. 2 B. 5 C. 1 D. 4

B. 5

What is the output of the following Java code? int x = 57; int y = 3; switch (x % 9) { case 0: case 1: y++; case 2: y = y - 2; break; case 3: y = y + 2; case 4: break; case 5: case 6: y = y + 3; } System.out.println(y); A. 2 B. 5 C. 6 D. 57

B. 5

What is y displayed in the following code? public class Test { public static void main(String[] args) { int x = 3; int y = x++ + x; System.out.println("y is " + y); } } A. 8 B. 7 C. 9 D. 4

B. 7

How many times will the following code print "Welcome to Java"? int count = 0; while (count < 8) { System.out.println("Welcome to Java"); count++; }//end of while loop A. 6 B. 8 C. 10 D. 7

B. 8

Suppose that s1, s2, and s3 are three strings, given as follows: String s1 ="Welcome to Java"; String s2 = "Programming is fun"; String s3 = "Welcome to Java"; What is the results of the following expressions? s1.concat(s2) A. Welcome Programming is fun B. Welcome to JavaProgramming is fun C. Welcome to Java Programming is fun D. WelcometoJavaProgrammingisfun

B. Welcome to JavaProgramming is fun

The method ____ is the first line of a method. A. statement B. declaration C. argument D. address

B. declaration

When a variable ceases to exist at the end of a method, programmers say the variable ____. A. is lost B. goes out of scope C. is out of memory range D. is undeclared

B. goes out of scope

Consider the following program. public class CircleArea { static Scanner console = new Scanner(System.in); static final float PI = 3.14; public static void main(String[]args) { float r; float area; r = console.nextDouble(); area = PI * r * r; System.out.println("Area = " + area); } } To successfully compile this program, which of the following import statement is required? A. import java.io; B. import java.util; C. import java.lang; D. No import statement is required

B. import java.util;

Which is an infinite loop? A. loopCount = 1; while(loopCount < 3); { System.out.println("Hello"); loopCount = loopCount + 1; } B. loopCount = 1; while(loopCount < 3); { System.out.println("Hello"); } C. loopCount = 5; while(loopCount > 3); { System.out.println("Hello"); loopCount = loopCount - 1; } D. loopCount = 4; while(loopCount < 3); { System.out.println("Hello"); loopCount = loopCount + 1; }

B. loopCount = 1; while(loopCount < 3); { System.out.println("Hello"); }

public static double secret(int first, double second) { double temp; if (second > first) temp = first * second; else temp = first - second; return temp; } What is the name of the above method? A. first B. secret C. double D. second

B. secret

Assume x=4, which of the following is true? A. x != 4 B. x != 5 C. x == 5 D. !(x == 4)

B. x != 5

Suppose that x is an int variable. Which of the following expressions always evaluates to false? A. (x > 0) || (x <= 0) B. (x > 0) || (x == 0) C. (x > 0) && ( x <= 0) D. (x >= 0) && (x == 0)

C. (x > 0) && ( x <= 0)

What is Math.floor(-33.6)? A. -30.0 B. -33.0 C. -34.0 D. -35.0

C. -34.0

After the execution of the following code, what will be the value of num if the input values are 0? (Assume that console is a Scanner object initialized to the standard input device.) int num = console.nextInt(); if (num > 0) num = num + 13; else if (num >= 3) num = num + 15; A. 13 B. 15 C. 0 D. 3

C. 0

public int mystery(int x, int y) { if (x >= y) return x - y; else return x + y; } Based on the code above, what would be the output of the following statement? System.out.println(mystery(8,7)); A. 8 B. 7 C. 1 D. 15

C. 1

The value of the expression 5 + 10 % 4 - 3 is ____. A. 0 B. 2 C. 4 D. 5

C. 4

Suppose x = 4 and y = 2. If the statement x *= y; is executed once, what is the value of x? A. 2 B. 4 C. 8 D. This is an illegal statement in Java.

C. 8

The expression (double)(5 + 4) evaluates to ____. A. 8 B. 9 C. 9.0 D. 10.0

C. 9.0

if (quotaAmt > 100 || sales > 100 && productCode == "C") bonusAmt = 50; When the above code is executed, which operator is evaluated first? A. || B. && C. == D. =

C. ==

Which of the following is true about a while loop? A. The body of the loop is executed at least once. B. The logical expression controlling the loop is evaluated before the loop is entered and after the loop exists. C. The body of the loop may not execute at all. D. It is a post-test loop

C. The body of the loop may not execute at all.

Data items you use in a call to a method are called ____. A. headers B. instance variables C. arguments D. method declarations

C. arguments

Suppose that alpha and beta are int variables. The statement alpha = --beta; is equivalent to the statement(s) ____. A. beta = beta - 1; alpha = 1 - beta; B. beta = beta - 1; alpha = beta - 1; C. beta = beta - 1; alpha = beta; D. alpha = beta; beta = beta - 1;

C. beta = beta - 1; alpha = beta;

A(n) ____ variable is known only within the boundaries of the method. A. double B. method C. local D. instance

C. local

A(n) ____ causes a value to be sent from a called method back to the calling method. A. method statement B. instantiation C. return statement D. inheritance relationship

C. return statement

public static double secret(int first, double second) { double temp; if (second > first) temp = first * second; else temp = first - second; return temp; } Which of the following is a valid call statement to the method given above? A. secret(int 5, double 4.8); B. secret(int x, double y); C. secret(5, 4.8); D. public static int secret(5, 4.8);

C. secret(5, 4.8);

Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative? A. (1 > x > 100) || (x < 0) B. 1 < x < 100 && x < 0 C. ( (x < 100) && (x > 1) ) && (x < 0) D. ( (x < 100) && (x > 1) ) || (x < 0)

D. ( (x < 100) && (x > 1) ) || (x < 0)

Suppose the following strings are declared as follows: String city = "Tallahassee"; String state = "Florida"; What is the results of the following expressions? city.concat(state).lastIndexOf('a') A. 10 B. Tallahassee Florida C. 2 D. 17 E. 15 F. TallahasseeFlorida

D. 17

Suppose that you have the following code: int sum = 0; int num = 8; if (num < 0) sum = sum + num; else if (num > 5) sum = num + 15; After this code executes, what is the value of sum? A. 0 B. 8 C. 15 D. 23

D. 23

public int mystery(int x, int y) { if (x >= y) return x - y; else return x + y; } Based on the code above, what would be the output of the following statement? System.out.println(mystery(8, mystery(2, 1)); A. 5 B. 11 C. 13 D. 7

D. 7

int x, y; if (x < 4) y = 2; else if (x > 4) { if (x > 7) y = 4; else y = 6; } else y = 8; Based on the code above, what is the value of y if x = 4? A. 2 B. 4 C. 6 D. 8

D. 8

What are the output of the following code segment: System.out.println(Math.round(8.5)); System.out.println(Math.rint(4.5)); System.out.println(Math.ceil(3.5)); System.out.println(Math.rint(9.5)); A. 8.0 4.5 3.0 9.0 B. 10 3.0 3.0 10.0 C. 8.0 5.0 4.0 9.0 D. 9 4.0 4.0 10.0

D. 9 4.0 4.0 10.0

System.out.println("Your name is " + yourName); The above statement is an example of ____, which is used to join Strings. A. buffering B. referencing C. parsing D. concatenation

D. concatenation

The ____ method executes first in an application, regardless of where you physically place it within its class. A. execute() B. start() C. run() D. main()

D. main()

Assume x = 4 and y = 5, which of the following is true? A. x < 5 && y < 5 B. x > 5 || y > 5 C. x > 5 && y > 5 D. x < 5 || y < 5

D. x < 5 || y < 5

What is the output of the following Java code? int x = 0; if (x > 0) System.out.println("positive "); System.out.print("zero "); System.out.println("negative"); A. zero B. negative C. positive zero negative D. zero negative

D. zero negative

If ++x is used in an expression, true or false; First the expression is evaluated, and then the value of x is incremented by 1.

False

Suppose that the input is 6 and console is a Scanner object initialized to the standard input device. Consider the following code. int alpha = 7; int beta = console.nextInt(); switch (beta) { case 5: alpha = alpha + 1; case 6: alpha = alpha + 2; case 7: alpha = alpha + 3; default: alpha = alpha + 5; } System.out.print(alpha); True or False; The output of this code is 9.

False

True or False; A computer program will recognize both = and == as the equality operator.

False

True or False; A method body provides information about how other methods can interact with it.

False

True or False; An identifier can be any sequence of characters and integers.

False

True or False; In Java, || has a higher precedence than &&.

False

True or False; The following for loop executes 21 times. (Assume all variables are properly declared.) for (i = 1; i <= 20; i = i + 1) System.out.println(i);

False

True or False; The output of the Java code, assuming that all variables are properly declared, is 32. num = 10; while (num <= 32) num = num + 5; System.out.println(num);

False

True or False; The output of the Java code, assuming that all variables are properly declared, is: 2 3 4 5 6. n = 2; while (n >= 6) { System.out.print(n + " "); n++; } System.out.println();

False

True or False; When nesting loops, the variable in the outer loop changes more frequently.

False

True or False; Will the following program terminate? int balance = 10; while (true) { if (balance < 9) continue; balance = balance - 9; }

False

Suppose x = 18.6. True or False; The output of the statement System.out.println((int)(x) / 3); is 6.

True

True or False; An application's main() method must have a void return type.

True

True or False; In the case of an infinite while loop, the while expression (that is, the loop condition) is always true.

True

True or False; The output of the following Java code is: Stoor. int count = 5; System.out.print("Sto"); do { System.out.print('o'); count--; } while (count >= 5); System.out.println('r');

True

True or False; When using the prewritten equals() method, a true or false comparison between two Strings is returned, but you do not know how the code looks behind the scenes.

True

True or False; The following four statements are equivalent? number += 1; number = number +1; number++; ++number;

True


Related study sets

CBK Domain 4 - Communication and Network Security

View Set

Chapter 43: Management of Patients With Musculoskeletal Trauma

View Set

Basic Tax Rules: Market Analysis

View Set

Graphing in a Variety of Contexts

View Set

Conflict mid-term chap 1, 2, 3, 4, 5

View Set

Learning, Teaching, and Assessment Final Exam

View Set