chapter 4 computer science

Ace your homework & exams now with Quizwiz!

What!is!the!value!of!the!following!expression?! 1 % 12

1

What output is produced by these statements? String name = "Joanne Hunt"; System.out.println(name.length());

11

What!will!be!printed!by!the!statement!below? System.out.print ("O\"my\t\\\"no!");

O"my \"no!

Which is the Java equivalent of the following mathematical expression? c = √(a2 + b2)

c = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));

Which one of the following statements can be used to get the fifth character from a string str?

char c = str.charAt(4);

What is the output of the following code snippet? public static void main(String[] args) { int s; double f = 365.25; s = f / 10; System.out.println(s); }

no output because compilation error

Assume!the!variable!str has!been!declared!to!be!a!String!that!has!at!least!one!character.!!! !!!!!!!Which!is!the!following!represents!the!last!character!in!str?! * str.charAt (str.length() - 1) * str.substring(str.length()) * str.charAt (str.length()) * str.lastChar()

str.charAt(str.length()-1)

How do you extract the first 5 characters from the string str?

str.substring(0,4)

Which one of the following statements can be used to extract the last five characters from any string variable str? * str.substring(str.length() - 4, 5) * str.substring(str.length() - 5, 5) * str.substring(str.length() - 5, str.length()) * str.substring(5, 5)

str.substring(str.length()-5, str.length())

What is wrong with the following code snippet? int average; average = 78A;

the average variable is assigned a non-numeric value

Which of the following statements with comments is(are) valid? I. int cnt = 0; /* Set count to 0 II. int cnt = 0; /* Set count to 0 */ III.int cnt = 0; // Set count to 0

II and III are valid

What does the following statement sequence print if the user input is 123? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); String str = in.next(); str += 456; System.out.println(str); }

123456

What will be the value stored in the variable x after the execution of the following code snippet? int a = 10; int b = 20; int c = 2; int x = b / a /*c*/;

2

What is the output of the following code snippet? System.out.printf("%5.3f", 20.0);

20.000

What is the output of the following code snippet? public static void main(String[] args) { double a; a = Math.sqrt(9.0) + Math.sqrt(16.0); System.out.println(a); }

7.0

What is the output of the following code snippet? public static void main(String[] args){ { String str1; str1 = "I LOVE MY COUNTRY"; String str2 = str1.substring(4, 11); System.out.println(str2); }

VE MY C

What is wrong with the following code? int count = 2000 * 3000 * 4000;

integer overflow

Which one of the following statements defines a constant with the value 123? * final int MY_CONST; MY_CONST = 123; * static int MY_CONST = 123; * final int MY_CONST = 123; * const int MY_CONST = 123;

final int MY_CONST = 123;

Which operator computes the remainder of an integer division?

%

Which one of the following is a correct representation of the given mathematical expression in Java? (a+b)/2...

(a+b)/2

What is the value inside the value variable at the end of the given code snippet? public static void main(String[] args) { int value = 3; value = value - 2 * value; value++; }

-2

Which of the methods below are static methods? I. length II. Substring III. pow III. 22 / 7.0 IV. sqrt

.pow .sqrt

What is the output of the following code snippet? public static void main(String[] args) { int num1 = 10; int num2 = 5; int num3 = 200; num3 = num3 % (num1 * num2); System.out.println(num3); }

0

Assume!the!following!variable!has!been!declared!and!given!a!value!as!shown: String str = "0123456789"; Which is the value of str.length()?

10

Assuming that the user inputs a value of 25 for the price and 10 for the discount rate in the following code snippet, what is the output? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the price: "); double price = in.nextDouble(); System.out.print("Enter the discount rate: "); double discount = in.nextDouble(); System.out.println("The new price is " + price - price * (discount / 100.0)); }

22.5

What is the output of the following code snippet? public static void main(String[] args) { int value = 3; value++; System.out.println(value); }

4

What is the output of the following code snippet? public static void main(String[] args) { int value = 25; value = value * 2; value--; System.out.println(value); }

49

What does the following statement sequence print if the user input is 123? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number "); int myInt = in.nextInt(); myInt += 456; System.out.println(myInt); }

579

What is the output of the following code snippet? String firstname = "William"; String lastname; System.out.println("First: " + first); System.out.println("Last: " + lastname);

Code will not compile

What does the following statement sequence print? String str = "Java Is Good"; int n = str.length(); String mystery = str.substring(n - 4, n) + str.charAt(4) + str.substring(0, 4); System.out.println(mystery);

Good Java

What does the following statement sequence print? String str = "Harry"; int n = str.length(); String mystery = str.substring(0, 1) + str.substring(n - 2, n); System.out.println(mystery);

Hry

Assuming that the user inputs "Joe" at the prompt, what is the output of the following code snippet? public static void main(String[] args) { System.out.print("Enter your name "); String name; Scanner in = new Scanner(System.in); name = in.next(); name += ", Good morning"; System.out.print(name); }

Joe, Good morning

Suppose a phone number, stored as a ten-character string (of digits only) called phoneNumber must be converted into a string that has parentheses around the area code. Which statement below will do that? * String newNumber = "(" + phoneNumber.substring(1, 3) + ")" + phoneNumber.substring(3, 7); * String newNumber = "(" + phoneNumber.substring(0, 3) + ")" + phoneNumber.substring(3, 10); * String newNumber = "(" + phoneNumber.substring(3, 0) + ")"; * String newNumber = "(" + ")" + phoneNumber;

String newNumber = "(" + phoneNumber.substring(0,3) + ")" + phoneNumber.substring (3,10);

Assume!the!following!variables!have!been!declared!and!given!values!as!shown: String str = "0123456789"; String sub = str.substring(3, 4); Which is the value of sub?

System.out.print(name.substring(7) + " " +name.substring (0,5));

Which one of the following statements displays the output as (1.23e+02)? * System.out.printf("^5.2e", -123.0); * System.out.printf("%5.2E", -123.0); * System.out.printf("%(5.2e", -123.0); * System.out.printf("%5.2e", -123.0);

System.out.printf("%5.2e", -123.0);

Which one of the following statements displays the output as +000321.00? * System.out.printf("+9.2f", 321.0); * System.out.printf("%09.00f", 321.0); * System.out.printf("+%09.2f", 321.0); * System.out.printf("%009,2f", 321.0);

System.out.printf("+%09.2f", 321.0);

Which one of the following statements displays the output as 54321.00? * System.out.printf(",8.2f", 54321.0); * System.out.printf("%8.00f", 54321.0); * System.out.printf("%8.2f", 54321.0); * System.out.printf("%8,2f", 54321.0);

System.out.println("%8.2f, 54321.0);

Which of the given System.out.print statements generates the following output? ABCDE"\

System.out.println("ABCDE\'\\");

Which of the given statements generates the following output? \\\"///

System.out.println("\\\\\\"///");

What will be the value inside the variables a and b after the given set of assignments? int a = 20; int b = 10; a = (a + b) / 2; b = a; a++;

a=16, b=15

Assume!the!following!variables!have!been!declared!and!given!values!as!shown: int i = 2345; double m = 67.8; What will be printed by the statement below? System.out.printf ("Values are %10d and %7.2f", i, j);

values are 2345 and 67.8

What is wrong with the following code snippet? public class Area { public static void main(String[] args) { int width = 10; height = 20.00; System.out.println("area = " + (width * height)); } }

code snippet has an undeclared variable

What is the result of the following code snippet? public static void main(String[] args) { double circleRadius; double circleVolume = 22 / 7 * circleRadius * circleRadius; System.out.println(circleVolume); }

compile-time error

Which!of!the!following!statements!will!assign!the!largest!value!of!three!integer!variables!a,!b,!and!c! to!the!integer!variable!maximum? * maximum = Math.max(a, b); maximum = Math.max(maximum, c); * maximum = Math.max(a, b) + Math.max(b, c) + Math.max(a, c); * maximum = Math.max(a, b, c); * maximum = Math.max(a, b); maximum = Math.max(b, c); maximum = Math.max(a, c);

maximum = Math.max(a,b); maximum = Math.max(maximum, c);

What does the following statement sequence print? final String str = "Java"; str += " is powerful"; System.out.println(str);

nothing; compile-time error

What are the values of num1 and num2 after this snippet executes? double num1 = 4.20; double num2 = num1 * 10 + 5.0;

num1 = 4.20 num2 = 47

Consider the following division statements: I. 22 / 7 II. 22.0 / 7 Which of the following is correct? * Only I, II will return an integer value. * Only I and III will return an integer value. * All three statements will return an integer value. * Only I will return an integer value.

only I will return an integer value

What is the output of the following code snippet? public static void main(String[] args) { int var1 = 10; int var2 = 2; int var3 = 20; var3 = var3 / (var1 % var2); System.out.println(var3);}

there will be no output due to a run-time error

Which of the following statements is correct about constants? * Constants are written using capital letters because the compiler ignores constants declared in small letters. * You can make a variable constant by using the final reserved word when declaring it. * The data stored inside a constant can be changed using an assignment statement. * Constant variables can only be changed through the Math library.

you can make a variable constant by using the "final" reserved word when declaring it.


Related study sets

Cognitive Psychology Chapter 6 & 7

View Set

BIOLOGY 346 Microbes and society

View Set

The Cell - Anatomy and Division Lab Assignment

View Set

fahmy 1 ( bible ) Arabic & German 7

View Set