CSCI Final Joshi

¡Supera tus tareas y exámenes ahora con Quizwiz!

Fix the order of the code. 1.) public class Welcome { 2.) System.out.println("Welcome to Java!"); 3.) } } 4.) public static void main(String[] args) {

1.) public class Welcome { 4.) public static void main(String[] args) { 2.) System.out.println("Welcome to Java!"); 3.) } }

The expression 4 + 20 / (3 - 1) * 2 is evaluated to:

24

Suppose x is 1. What is x after x += 2?

3

24 % 5 in Java is _____

4

Math.pow(2, 3) returns __________.

8.0

____________ is the Java assignment operator.

=

If you forget to put a closing quotation mark on a string, then the program suffers from this error.

A compile error

If a program compiles fine, but it produces incorrect result, then the program suffers from this error.

A logic error

What is the return value of "SELECT".substring(0, 4)? A.) "SELE" B.) "SELECT" C.) "SEL" D.) "LECT"

A.) "SELE"

Which of the following is a valid identifier? A.) $343 B.) 9X C.) class D.) radius

A.) $343 D.) radius

Which of the following lines is not a Java comment? A.) ** comments ** B.) /* comments */ C.) -- comments D.) // comments

A.) ** comments ** C.) -- comments

What is the output of the following code? double[] myList = {1, 5, 3, 4, 5, 2}; double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; } } System.out.println(indexOfMax); A.) 1 B.) 0 C.) 2 D.) 5

A.) 1

If you enter 1, 2, and 3, when you run this program, what will be the output? import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter three numbers: "); double number1 = input.nextDouble(); double number2 = input.nextDouble(); double number3 = input.nextDouble(); // Compute average double average = (number1 + number2 + number3) / 3; // Display result System.out.println(average); } } A.) 2.0 B.) 4.0 C.) 1.0 D.) 3.0

A.) 2.0

Which of the following expression results in a value 1? A.) 37 % 6 B.) 25 % 5 C.) 2 % 1 D.) 15 % 4

A.) 37 % 6

After the execution of the following code, what is i printed as? public class Test { public static void main(String[] args) { int j = 0; int i = ++j + j * 5; System.out.println("What is i? " + I); } } A.) 6 B.) 5 C.) 0 D.) 1

A.) 6

The goal is to extract a user mention (@xyz) from a tweet text. Put the code below in the correct order. A.) Scanner input = new Scanner(System.in); B.) System.out.println("Enter a tweet: "); C.) String tweet = input.nextLine(); D.) int indexAt = tweet.indexOf('@'); E.) int indexSp = tweet.indexOf(' ', indexAt); F.) String userMention = tweet.substring(indexAt, indexSp); G.) System.out.println("User Mention = " + userMention);

A.) Scanner input = new Scanner(System.in); B.) System.out.println("Enter a tweet: "); C.) String tweet = input.nextLine(); D.) int indexAt = tweet.indexOf('@'); E.) int indexSp = tweet.indexOf(' ', indexAt); F.) String userMention = tweet.substring(indexAt, indexSp); G.) System.out.println("User Mention = " + userMention);

The goal of the code below is to find the sum of the digits within a 3 digit number. For example, if the user enters 345, the output should be 12 as the sum. Put the code in the correct order. A.) Scanner input = new java.util.Scanner(System.in); B.) System.out.print("Enter an integer between 0 and 1000: "); C.) int number = input.nextInt(); D.) int remainingNumber = number / 10; E.) remainingNumber = remainingNumber / 10; F.) int secondLastDigit = remainingNumber % 10; G.) int thirdLastDigit = remainingNumber % 10; H.) int lastDigit = number % 10; I.) nt sum = lastDigit + secondLastDigit + thirdLastDigit; J.) System.out.println("The sum of all digits in " + number + " is " + sum);

A.) Scanner input = new java.util.Scanner(System.in); B.) System.out.print("Enter an integer between 0 and 1000: "); C.) int number = input.nextInt(); H.) int lastDigit = number % 10; D.) int remainingNumber = number / 10; F.) int secondLastDigit = remainingNumber % 10; E.) remainingNumber = remainingNumber / 10; G.) int thirdLastDigit = remainingNumber % 10; I.) int sum = lastDigit + secondLastDigit + thirdLastDigit; J.) System.out.println("The sum of all digits in " + number + " is " + sum);

Analyze the following code. public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println("x[0] is " + x[0]); } } A.) The program runs fine and displays x[0] is 0. B.) The program has a runtime error because the array element x[0] is not defined. C.) The program has a compile error because the size of the array wasn't specified when declaring the array. D.) The program has a runtime error because the array elements are not initialized.

A.) The program runs fine and displays x[0] is 0.

Which of the following assignment statements is correct? A.) char c = 'd'; B.) char c = 100; C.) char c = "100"; D.) char c = "d";

A.) char c = 'd'; B.) char c = 100;

Which of the following are correct names for variables according to Java naming conventions? A.) findArea B.) RADIUS C.) Radius D.) FindArea E.) radius

A.) findArea E.) radius

Write a program that prompts the user to enter a three-digit integer and determines whether it is a palindrome integer. An integer is palindrome if it reads the same from right to left and from left to right. Order the code below for the solution. A.) import java.util.Scanner; public class IsPalindrome { public static void main(String[] args) { Scanner input = new Scanner(System.in); B.) else System.out.println(number + " is not a palindrome"); C.) System.out.print("Enter a three-digit integer: "); int number = input.nextInt(); D.) } } E.) if (number / 100 == number % 10) System.out.println(number + " is a palindrome");

A.) import java.util.Scanner; public class IsPalindrome { public static void main(String[] args) { Scanner input = new Scanner(System.in); C.) System.out.print("Enter a three-digit integer: "); int number = input.nextInt(); E.) if (number / 100 == number % 10) System.out.println(number + " is a palindrome"); B.) else System.out.println(number + " is not a palindrome"); D.) } }

The goal is the swap the values stored in num1 and num2. Put the code in the correct order. A.) int num1 = 100; B.) int num2 = 200; C.) num1 = temp; D.) num2 = num1; E.) int temp = num2;

A.) int num1 = 100; B.) int num2 = 200; E.) int temp = num2; D.) num2 = num1; C.) num1 = temp;

To declare an int variable number with initial value 2, you write A.) int number = 2; B.) int number = 2L; C.) int number = 2l; D.) int number = 2.0;

A.) int number = 2;

The following loop initializes the array with user input values, Put in the correct order. A.) java.util.Scanner input = new java.util.Scanner(System.in); B.) for (int column = 0; column < matrix[row].length; column++) { C.) matrix[row][column] = input.nextInt(); D.) for (int row = 0; row < matrix.length; row++) { E.) System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + " columns: "); F.) } }

A.) java.util.Scanner input = new java.util.Scanner(System.in); E.) System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + " columns: "); D.) for (int row = 0; row < matrix.length; row++) { B.) for (int column = 0; column < matrix[row].length; column++) { C.) matrix[row][column] = input.nextInt(); F.) } }

The following code displays ___________. double temperature = 50; if (temperature >= 100) System.out.println("too hot"); else if (temperature <= 40) System.out.println("too cold"); else System.out.println("just right"); A.) just right B.) too hot too cold just right C.) too cold D.) too hot

A.) just right

To assign a value 1 to variable x, you write: A.) x = 1; B.) 1 = x; C.) x == 1; D.) x := 1;

A.) x = 1;

What is the output of the following code? int x = 0; if (x < 4) { x = x + 1; } System.out.println("x is " + x); A.) x is 1 B.) x is 4 C.) x is 0 D.) x is 3

A.) x is 1

Will the following program terminate? int balance = 10; while (true) { if (balance < 9) break; balance = balance - 9; } A.) yes B.) no

A.) yes

Which of the following is the correct statement to return JAVA? A.) String.toUpperCase("Java") B.) "Java".toUpperCase() C.) "Java".toUpperCase("Java") D.) toUpperCase("Java")

B.) "Java".toUpperCase()

The extension name of a Java source code file is A.) .obj B.) .java C.) .exe D.) .class

B.) .java

What is the output of the following code? public class Test { public static void main(String[] args) { int[][] matrix = {{1, 2, 3, 4}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; for (int i = 0; i < 4; i++) System.out.print(matrix[i][1] + " "); } } A.) 1 2 3 4 B.) 2 5 9 13 C.) 1 3 8 12 D.) 4 5 6 7

B.) 2 5 9 13

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________. A.) 3.4 B.) 2.0 C.) 3.5 D.) 5.5

B.) 2.0

If you enter 1 2 3, when you run this program, what will be the output? import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter three numbers: "); double number1 = input.nextDouble(); double number2 = input.nextDouble(); double number3 = input.nextDouble(); // Compute average double average = (number1 + number2 + number3) / 3; // Display result System.out.println(average); } } A.) 3.0 B.) 2.0 C.) 1.0 D.) 4.0

B.) 2.0

Which of the following is correct to obtain a random integer between 5 and 10? A.) 5 + Math.random() * 6 B.) 5 + (int)(Math.random() * 6) C.) 5 + (int)(Math.random() * 5) D.) 5 + Math.random() * 5

B.) 5 + (int)(Math.random() * 6)

Math.pow(2, 3) returns __________. A.) 9 B.) 8.0 C.) 9.0 D.) 8

B.) 8.0

What is Math.round(8.6)? A.) 8.5 B.) 9 C.) 9.0 D.) 8.0

B.) 9

What is the purpose of the code below? Scanner input = new Scanner(System.in); int number, max; number = input.nextInt(); max = number; do { number = input.nextInt(); if (number > max) max = number; } while (number != 0); A.) Not sure B.) Allow the user to enter a series of numbers until the user enters a 0, and find the biggest number the user entered. C.) Nothing. The code will not work. D.) Allow the user to enter a series of numbers and find the biggest number the user entered.

B.) Allow the user to enter a series of numbers until the user enters a 0, and find the biggest number the user entered.

The __________ method parses a string s to a double value. A.) Double.parsedouble(s); B.) Double.parseDouble(s); C.) double.parseDouble(s); D.) double.parse(s);

B.) Double.parseDouble(s);

Which of the following statements is correct to display Welcome to Java on the console? A.) System.out.println('Welcome to Java"); B.) System.out.println("Welcome to Java"); C.) System.println('Welcome to Java'); D.) System.out.println('Welcome to Java');

B.) System.out.println("Welcome to Java");

Goal: Summing elements by column. Put the code in the correct order. A.) int total = 0; B.) for (int column = 0; column < matrix[0].length; column++) { C.) }//column D.) for (int row = 0; row < matrix.length; row++){ E.) }//row F.) total += matrix[row][column]; G.) System.out.println("Sum for column " + column + " is " + total);

B.) for (int column = 0; column < matrix[0].length; column++) { A.) int total = 0; D.) for (int row = 0; row < matrix.length; row++){ F.) total += matrix[row][column]; E.) }//row G.) System.out.println("Sum for column " + column + " is " + total); C.) }//column

Suppose a Scanner object is created as follows, what method do you use to read a real number (with decimal)? Scanner input = new Scanner(System.in); A.) input.nextdouble(); B.) input.nextDouble(); C.) input.Double(); D.) input.double();

B.) input.nextDouble();

Suppose a Scanner object is created as follows, what method do you use to read a real number? Scanner input = new Scanner(System.in); A.) input.double(); B.) input.nextDouble(); C.) input.Double(); D.) input.nextdouble();

B.) input.nextDouble();

The goal for the following code is to display "Welcome to Java" 10 times. Put the code in the correct order. A.) while (count < 10) { B.) int count = 0; C.) count++; D.) System.out.println("Welcome to Java"); E.) }

B.) int count = 0; A.) while (count < 10) { D.) System.out.println("Welcome to Java"); C.) count++; E.) }

The goal is to swap the two numbers. Put the code below in the correct order to accomplish this goal. A.) num1 = temp; B.) int num1 = 100; C.) int num2 = 200; D.) num2 = num1; E.) int temp = num2;

B.) int num1 = 100; C.) int num2 = 200; E.) int temp = num2; D.) num2 = num1; A.) num1 = temp;

Suppose s is a string with the value "java is amazing". What will be assigned to x if you execute the following code? char x = s.charAt(14); A.) 'n' B.) Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException C.) 'g'

C.) 'g'

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.) ((x < 100) && (x > 1)) && (x < 0) C.) ((x < 100) && (x > 1)) || (x < 0) D.) 1 < x < 100 && x < 0

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

To check whether a char variable ch is an uppercase letter, you write ___________. A.) (ch >= 'A' && ch >= 'Z') B.) ('A' <= ch <= 'Z') C.) (ch >= 'A' && ch <= 'Z') D.) (ch >= 'A' || ch <= 'Z')

C.) (ch >= 'A' && ch <= 'Z')

Which of the following is a possible output from invoking Math.random()? A.) 1.1 B.) 3.43 C.) 0.5 D.) 1.0

C.) 0.5

How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) System.out.println(i * j); A.) 50 B.) 45 C.) 100 D.) 10

C.) 100

What is the result of 45 / 4 in Java? A.) 10 B.) 12 C.) 11 D.) 11.25

C.) 11

public class Test { public static void main(String[] args) { System.out.println("3.5 * 4 / 2 - 2.5 is "); System.out.println(3.5 * 4 / 2 - 2.5); } } A.) 3.5 * 4 / 2 - 2.5 is 4.5 B.) None of the above C.) 3.5 * 4 / 2 - 2.5 is 4.5 D.) 4.5

C.) 3.5 * 4 / 2 - 2.5 is 4.5

What is the output for y? int y = 0; for (int i = 0; i &lt; 10; ++i) { y += i; } System.out.println(y); A.) 11 B.) 10 C.) 45 D.) 43

C.) 45

____________ is the Java assignment operator. A.) =: B.) == C.) = D.) :=

C.) =

The equal comparison operator in Java is __________. A.) <> B.) ^= C.) == D.) !=

C.) ==

The following code fragment reads in two numbers. What is the incorrect way to enter these two numbers? Scanner input = new Scanner(System.in); int i = input.nextInt(); double d = input.nextDouble(); A.) Enter an integer, a space, a double value, and then the Enter key. B.) Enter an integer, an Enter key, a double value, and then the Enter key. C.) Enter a numeric value with a decimal point, a space, an integer, and then the Enter key. D.) Enter an integer, two spaces, a double value, and then the Enter key.

C.) Enter a numeric value with a decimal point, a space, an integer, and then the Enter key.

Given the code: Scanner input = new Scanner(System.in); System.out.print("Enter a string: "); String s = input.nextLine(); for(int i = 0; i < s.length(); i++){ System.out.print(s.charAt(i) + "\t"); } What is the output if the user enters "Hello"? A.) H e l l o B.) Hello C.) H e l l o D.) H e l l o

C.) H e l l o

________ contains predefined classes and interfaces for developing Java programs. A.) Java language specification B.) Java IDE C.) Java API D.) Java JDK

C.) Java API

_________ is a software that interprets Java bytecode. A.) Java debugger B.) Java API C.) Java virtual machine D.) Java compiler

C.) Java virtual machine

What is the return value of "SELECT".substring(4, 4)? A.) T B.) E C.) an empty string D.) C

C.) an empty string

What is the output after the following loop terminates? int number = 25; int i; boolean isPrime = true; for (i = 2; i < number; i++) { if (number % i == 0) { isPrime = false; break; } } System.out.println("i is " + i + " isPrime is " + isPrime); A.) i is 6 isPrime is false B.) i is 5 isPrime is true C.) i is 5 isPrime is false D.) i is 6 isPrime is true

C.) i is 5 isPrime is false

Which of the following code displays the area of a circle if the radius is positive? A.) if (radius >= 0) System.out.println(radius * radius * 3.14159); B.) if (radius != 0) System.out.println(radius * radius * 3.14159); C.) if (radius > 0) System.out.println(radius * radius * 3.14159); D.) if (radius <= 0) System.out.println(radius * radius * 3.14159);

C.) if (radius > 0) System.out.println(radius * radius * 3.14159);

Our goal is to write a program that prompts the user to enter the side of a hexagon and displays its area. The formula for computing the area of a hexagon is Area = 3√3 / 2 s2 where s is the length of a side. Put the code below in the proper order to get a solution. A.) Scanner input = new Scanner(System.in); B.) public class HexArea { C.) import java.util.Scanner; D.) public static void main(String[] args) { E.) System.out.println("The area of the hexagon is " + area); F.) double side = input.nextDouble(); G.) System.out.print("Enter the length of the side: "); H.) } } I.) double area = 3 * Math.pow(3, 0.5) * side * side / 2;

C.) import java.util.Scanner; B.) public class HexArea { D.) public static void main(String[] args) { A.) Scanner input = new Scanner(System.in); G.) System.out.print("Enter the length of the side: "); F.) double side = input.nextDouble(); I.) double area = 3 * Math.pow(3, 0.5) * side * side / 2; E.) System.out.println("The area of the hexagon is " + area); H.) } }

Which of these data types requires the most amount of memory? A.) byte B.) int C.) long D.) short

C.) long

For the binarySearch method in the book and discussed in class, what is low, high and mid at the third iteration of the while loop when invoking binarySearch(new int[]{1, 4, 6, 8, 10, 15, 20, 21, 39, 81}, 15)? Array = [1, 4, 6, 8, 10, 15, 20, 21, 39, 81] [Key = 15] A.) low is 15, high is 20, mid is 20 B.) low is 1, high is 81, mid is 10 C.) low is 15, high is 20, mid is 15 D.) low is 1, high is 8, mid is 6 E.) low is 15, high is 81, mid is 21

C.) low is 15, high is 20, mid is 15

Which of the following is a valid identifier? A.) class B.) 9X C.) radius D.) $343 E.) 8+9

C.) radius D.) $343

Consider the following incomplete code. The missing method body should be ________. public class Test { public static void main(String[] args) { System.out.println(f(5)); } public static int f(int number) { // Missing body } } A.) System.out.println(number); B.) System.out.println("number"); C.) return number; D.) return "number";

C.) return number;

Suppose int x = 3264, what is the output of the following code? int y = x % 10; x = x / 10; System.out.println("x is " + x + " and y is " + y); A.) x is 3264 and y is 4 B.) x is 3264 and y is 326.4 C.) x is 326 and y is 4 D.) x is 326 and y is 326

C.) x is 326 and y is 4

What is the output of the following code: double x = 5.5; int y = (int)x; System.out.println("x is " + x + " and y is " + y); A.) x is 6.0 and y is 6.0 B.) x is 6 and y is 6 C.) x is 5.5 and y is 5 D.) x is 5 and y is 6

C.) x is 5.5 and y is 5

The brain of a computer

CPU

In the following code, what is the output for list2? public class Test { public static void main(String[] args) { int[] list1 = {1, 2, 3}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list2.length; i++) System.out.print(list2[i] + " "); } } A.) 0 1 3 B.) 1 2 3 C.) 1 1 1 D.) 0 1 2

D.) 0 1 2

What is the value of balance after the following code is executed? int balance = 10; while (balance >= 1) { if (balance < 9) break; balance = balance - 9; } A.) 9 B.) 0 C.) -1 D.) 1

D.) 1

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

D.) 1 3 5 7 9

The Unicode of 'c' is 99. What is the Unicode for 'f'? A.) 100 B.) 101 C.) 99 D.) 102

D.) 102

"abc".compareTo("aba") returns ___________. A.) -1 B.) True C.) 0 D.) 2

D.) 2

What is y after the following switch statement is executed? int x = 3; int y = 4; switch (x + 3) { case 6: y = 0; case 7: y = 1; default: y += 1; } A.) 0 B.) 3 C.) 1 D.) 2

D.) 2

The expression 4 + 20 / (3 - 1) * 2 is evaluated to A.) 25 B.) 4 C.) 20 D.) 24

D.) 24

Assume double[][] x = new double[4][5], what are x.length and x[2].length? A.) 4 and 4 B.) 5 and 5 C.) 5 and 4 D.) 4 and 5

D.) 4 and 5

What is Math.rint(7.6)? A.) 7 B.) 7.5 C.) 7.0 D.) 8.0

D.) 8.0

If the following code is executed, and the user enters "Hello". What will the output be? Scanner input = new Scanner(System.in); System.out.print("Enter a character: "); String s = input.nextLine(); char ch = s.charAt(0); System.out.println("The character entered is " + ch); A.) Hello B.) Error message C.) Nothing D.) H

D.) H

Suppose income is 4001, what is the output of the following code? if (income > 3000) { System.out.println("Income is greater than 3000"); } else if (income > 4000) { System.out.println("Income is greater than 4000"); } A.) Income is greater than 3000 followed by Income is greater than 4000 B.) No output C.) Income is greater than 4000 D.) Income is greater than 3000

D.) Income is greater than 3000

The expression "Java " + 1 + 2 + 3 evaluates to ________. A.) Java123 B.) Java6 C.) Illegal expression D.) Java 123

D.) Java 123

Which of the following statement prints \smith\exam1\test.txt? A.) System.out.println("smith\exam1\test.txt"); B.) System.out.println("\smith"\exam1"\test.txt"); C.) System.out.println("smith\\exam1\\test.txt"); D.) System.out.println("\\smith\\exam1\\test.txt");

D.) System.out.println("\\smith\\exam1\\test.txt");

Are the following four statements equivalent? number += 1; number = number + 1; number++; ++number; A.) Only the last two B.) Only the first two C.) No D.) Yes

D.) Yes

What is the index variable for the element at the first row and first column in array a? A.) a[1][1] B.) a[1][0] C.) a[0][1] D.) a[0][0]

D.) a[0][0]

What is the exact output of the following code? double area = 3.5; System.out.print("area"); System.out.print(area); A.) 3.53.5 B.) area 3.5 C.) 3.5 3.5 D.) area3.5

D.) area3.5

To declare a constant MAX_LENGTH inside a method with value 99.98, you write A.) double MAX_LENGTH = 99.98; B.) final float MAX_LENGTH = 99.98; C.) final MAX_LENGTH = 99.98; D.) final double MAX_LENGTH = 99.98;

D.) final double MAX_LENGTH = 99.98;

The speed of the CPU may be measured in

Gigahertz

A technical definition of the language that includes the syntax and semantics of the Java programming language

Java language specification

Computer can execute the code in

Machine language

Analyze the following code: public class Test { public static void main(String[] args) { double[] x = {2.5, 3, 4}; for (double value: x) System.out.print(value + " "); } } A.) The program displays 2.5, 3.0 4.0 B.) The program displays 2.5, 3, 4 C.) The program displays 2.5 3 4 The program displays 2.5 3.0 4.0

The program displays 2.5 3.0 4.0

If you forget to put a closing quotation mark on a string, what kind of error will be raised?

a compile error

If a program compiles fine, but it produces incorrect result, then the program suffers __________

a logic error

Every statement in Java ends with ________.

a semicolon (;)

The code doesn't run

compile error

The code runs but gives incorrect output

logic error

Computer can execute the code in ____________.

machine language

The main method header is written as:

public static void main(String[] args)

The code begins to run but crashes

runtime error

What is incorrect in the following lines of code? public class TestMain { public ststic void main (string[] args) { System.out.println("Hello World!"); } }

string should be String


Conjuntos de estudio relacionados

Sleeman Midterm 1 : WWU Econ 206

View Set

doing nothing is something (review questions)

View Set

chapter 1 life and health insurance exam

View Set