Unit 1: Primitive Types

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

Consider the following code. public static void main(String args[]) { double rate = 5; double amount = 500; double discount = amount*(rate/100); System.out.print(" ******** "); System.out.print(" Calculate Discount "); System.out.print(" ******** "); System.out.println(); System.out.print(" "+rate+"%"+" discount on $"+amount); System.out.print(" is $"+discount); } Which of the following best describes the output of the above code segment?

(A) ******** Calculate Discount ******** 5.0% discount. on. $500.0 is $25.

Consider the following code segment. int x = -1; int y = 7; int z = 0; z = y + x * -4; y += z; x -= 3; z--; System.out.println(z); What is printed when the code segment is executed?

(A) 10

What value is stored in the following variable named answer? int answer = 19/8%7+20/5*4+3-26%5;

(A) 20

Consider the following method. public static void doMath() { int a = -8/5; int b = 11/2; double c = 14/4; System.out.println(a+b+c); } What is the output when doMath() is called?

(A) 7.0

How does Computer Science benefit society? I. Computer science can be used to identify and investigate patterns of behavior. II. Computer science allows experts to leverage their knowledge with the use of computer applications. III. Computer science helps solve problems by stating a problem in a logical way so that it can be solved more systematically.

(A) I, II, and III

Assuming result is always a positive double, consider the following code segment which is intended to round result to the nearest int. int rounded = (int) (result + 0.5); System.out.println(rounded); Does the code segment work as intended?

(A) The code segment works as intended.

Why is the double data type named so?

(A) The data type represents a double-precision floating point number.

Consider the following code segment. double x = 19/5; System.out.println(x); Which of the following can replace the first line of code without changing the output?

(A) double x = (double) (19/5);

Consider the following code segment. public static void main(String args[]){ int n1=7; int n2= 15; n1++; n2--; System.out.println(" n1: "+ n1 + " n2: " + n2); n1+=n2; n2*=n1; System.out.println(" n1: "+ n1 + " n2: " + n2); } Which of the following describes the output of the above code?

(A) n1: 8 n2: 14 n1: 22 n2: 308

You are asked to create a class to define an Employee. One piece of information that needs to be tracked for each employee is his/her primary cell phone number (including the country and area code). Of the choices given, which would be the BEST option for declaring a variable to store this information?

(A) private String cellPhone;

Consider the following code. public static void main (String args[]){ System.out.println(" About System Class: "); System.out.print(" The System class"); System.out.println(" extends the Object class,"); System.out.print(" and it cannot be instantiated."); } Which of the following best describes the output of the above code segment?

(B) About System Class: The System class extends the Object class, and it cannot be instantiated.

Consider the following code segment. int a=3, b=4, c=0; c += b + a * 2; System.out.println(c); What is printed when the code segment is executed?

(B) 10

Consider the following method: public static void doMath() { int x = 6/3; int y = 8/3; int z = x*3+y*3; System.out.println(z); } What is the output when doMath() is called?

(B) 12

Consider the following code segment. int a=5, b=7, c=4; b %= 3; a--; c += 5; b *= a + c; System.out.println(b); What is printed when the code segment is executed?

(B) 13

Consider the following code segment. int a=3, b=8, c=18; c %= 4; b += a*c; System.out.println(b); What is printed when the code segment is executed?

(B) 14

Consider the following code segment. private int numOfBees = 32000; private int numOfFlowers = 8000; What is the maximum value that can be assigned to numOfBees?

(B) 2147483647

What is the output of the following code segment? int x = 15+2*3+7/4; System.out.println(x);

(B) 22

Consider the following code segment. public static void main(String args[]){ int penny=1; int dime=10; int quarter=25; int total=0; penny++; penny+=4; dime+=10; total=penny+dime+quarter; dime/=10; quarter/=25; System.out.println(penny+" pennies, "+dime+" dimes, "+quarter+" quarters: "+" total "+total+" cents"); } Which of the following best describes the output of the above code?

(B) 6 pennies, 2 dimes, 1 quarters: total 51 cents

Consider the following code segment. int a = 1, b = 4, c = 0; a--; b--; b += 4; c = b + a * 2; System.out.println(c); What is printed when the code segment is executed?

(B) 7

The following method, when completed, will compute and return the sum of all the odd values in the array numbers. public static int sumOdds(int[] numbers) { //precondition: the elements of numbers are all >0 int sum=0; for (int i=0; i<numbers.length; i++) { if (numbers[i] % 2==1) { //missing code } } return sum; } Which of the code segments below can be used to replace //missing code so that the method sumOdds works as intended? I. sum=sum+numbers[i]; II. sum+=numbers[i]; III. sum=+numbers[i];

(B) I and II

Which of the following will result in a "type mismatch" error? I. int total = 0; double dollarAmt = 10.56; total = total + (int) dollarAmt; II. double a = 22.0, b = 2.0; int age = a / (int) b; III. int[] nums = new int[10]; double num = nums[0];

(B) II only

The program below will not compile because it contains four syntax errors. Line 01: public class Birthday Line 02: { Line 03: Public static void main(String[] args) Line 04: { Line 05: system.out.print('Happy Birthday!'); Line 06: } Which of the following identifies and properly corrects all four of the syntax errors?

(B) Line 03: Public should be public Line 05: system should be System and Happy Birthday should be in double quotes After Line 06: There should be a "}"

Consider the following code segment. int x = 1; double y = x; double z = y / 2; System.out.println(z); What is printed when the code segment is executed?

(C) 0.5

Consider the following code segment. int num1=3, num2=5, num3=4; System.out.println( num1/num3-num2*num1/(num3+1)+10/4*2); What is printed to the console as a result of executing the code segment?

(C) 1

What is dubs initialized to? double dubs = (int)2.5*3+10-3/2+2.8;

(C) 17.8

Consider the following code segment. public static void mystery(int num) { //precondition: num>0 int x = 0; while (num > 0) { x += num % 10; System.out.print(x + " "); num /= 10; } } What is the result of the call mystery(12345)?

(C) 5 9 12 14 15 is printed.

Consider the following code segment. System.out.print((double) (10 / 4) * (int) 10.0 / 4 + (double) 10 / 4); What is printed as a result of executing the code segment?

(C) 7.5

Which of the provided options prints the following message on ONE line? Life is what happens when you're busy making other plans? I. System.out.println("Life is what happens when you're busy making other plans."); II. System.out.println("Life is what happens when "); System.out.println("you're busy making other plans."); III. System.out.print("Life is what happens when "); System.out.print("you're busy making other plans.");

(C) I and III.

Consider the following statements: I. x**; II. x++; III. x--; Which are valid Java statements?

(C) II and III only

Consider the following data types. I. String II. int III. boolean IV. double Which of the choices above are considered primitive data types in Java?

(C) II, III, and IV only.

Given that: double d = 10.0; int i = 20; Which of the following are valid statements which will compile successfully? I. int num = i / d; II. i = d; III. d = i / (int) d;

(C) III only.

Consider the following code. Line 01: public static void main(String args[]){ Line 02: System.out.print(" Hello!"); Line 03: system.out.println("I am a web server,who are you?"); Line 04: System.out.println(" I am a user computer , wants specific pages from you.); Line 05: System.out.Println("I can certainly help you with that."); Line 06: } Which lines of the code segment have syntax errors?

(C) Lines 03, 04, 05.

Consider the following code. public static void main(String args[]){ int p=5; int q=8; int r=20; q-=p; System.out.println("q="+q); p+1; r%=q; System.out.println("r="+r); } Which of the following best describes the behavior of the code?

(C) The code would throw a compilation error.

Consider the following code segment. final double INCOME_TAX_PERCENT = 7.60; // 7.60% int income = // assume that a valid income has been assigned; if (income > 25000) INCOME_TAX_PERCENT = INCOME_TAX_PERCENT +.01; double incomeTax = income * INCOME_TAX_PERCENT / 100; System.out.println(incomeTax); Assume that the income tax calculation is a straight-forward calculation of income times the income tax percentage. Which of the following describes why this code will not correctly print the income tax?

(C) The value initially assigned to INCOME_TAX_PERCENT cannot be changed.

Consider the following code. System.out.print("red"); System.out.println("green"); System.out.print("blue"); System.out.println("yellow"); System.out.print("pink"); Which of the following is printed as a result of running these commands

(C) redgreen blueyellow pink

Consider the following code segment. public static void main(String args[]){ int x=4; int y=5; int z=6; x+=y; z-=y; y*=x; int a=y; a/=5; int b=y; b%=a; System.out.println(" x is : "+ x); System.out.println(" y is : "+ y); System.out.println(" z is : "+ z); System.out.println(" a is : "+ a); System.out.println(" b is : "+ b); } Which of the following best describes the output of the above code?

(C) x is : 9 y is : 45 z is : 1 z is : 9 b is : 0

The Java type int uses four bytes for storage data. Integer.MIN_VALUE finds the smallest nonnegative int value. Which of the following is the correct formula to determine the minimum value?

(D) -2^31

Consider the following code segment. int a = 20; int b = 5; int c = 4; System.out.println(a/b+a/c+b/a+b/c+c/a+c/b); What is printed as a result of executing the code segment?

(D) 10

Consider the following code segment. int a=1, b=4, c=0; c = b + a*2; b++; a = c*(b-1) + 7%3; System.out.println(a); What is printed when the code segment is executed?

(D) 25

Consider the following code segment. int x = 5; x *= 2; x = x * 2 + x; System.out.println(x); What is printed when the code segment is executed?

(D) 30

Consider the following code segment. System.out.println(9/2.0+15/6+5.0/2); What is printed as a result of executing the code segment?

(D) 9.0

In java, an int datatype is stored in 32 bits. What is the maximum value for an int data type?

(D) Approximately 2.15 billion.

Which of the following are true statements about a constant? I. After a constant is initialized with a value, its value can only be changed by methods in the class it is defined in. II. SALES_TAX_RATE follows the proper naming convention for a constant variable. III. The keyword "final" means that the variable is a constant.

(D) II and III only.

Consider the following code. Line 01: public static void main(String args[]){ Line 02: String name =" Robin Smith"; Line 03: int age= 15; Line 04: String grade ="Tenth"; Line 05: Line 06: System.out.println(************STUDENT RECORD**************"); Line 07: System.out.Println("Student Name : "+ name); Line 08: System.out.println("Student Age: + age); Line 09: System.out.print("Student Grade:"+ grade); Line 10: } Which lines of the code segment have errors?

(D) Lines 06, 07, and 08.

Refer to the following code segment: int a = 13; double b = a; double answer = b / 5; System.out.println("13 / 5 = " + answer); The output is: 13 / 5 = 2.6 The programmer intends the output to be 13 / 5 = 2.0 What type of error is this?

(D) Logical error

A programmer is running a line of code, System.out.println(Integer.MAX_VALUE * 2); and the output is -2. This is not the expected output. What type of error is this?

(D) Overflow error

What is printed when the following code is executed? System.out.println("Tell me and I forget."); System.out.println(); System.out.println("Teach me and I remember."); System.out.println(); System.out.println("Involve me and I learn.");

(D) Tell me and I forget. Teach me and I remember. Involve me and I learn.

A jar contains coins only, with the largest coin being a quarter. The money in the jar is comprised of the fewer coins possible (e.g. there would be no more than 1 nickel, since 2 would make a dime). Consider the following method which is passed a positive amount of money in the jar in cents. public static int doIt (int jar) { jar %= 25; jar %= 10; jar %= 5; return jar; } Which of the following best describes what the method does?

(D) The method returns the amount of pennies in the jar.

Consider the following code segment. int x= -1, y=7, c=0; x -= 3; y += x; c = y + x * -4; System.out.println("Value of x is " + x); System.out.println("Value of y is " + y); System.out.println("Value of c is " + c); What is printed when the code segment is executed?

(D) Value of x is -4 Value of y is 3 Value of c is 19

All of the following code segments produce a compile-time error EXCEPT:

(D) int count = 0, sum = 200; int average = sum / count;

The Java type int uses four bytes for storage data. Integer.MAX_VALUE finds the largest nonnegative int value. Which of the following is the correct formula to determine the maximum value?

(E) 2^31 - 1

Consider the following code segment. double x = 2.5; int y = (int) x * 4; System.out.print(y); What is printed when the code segment is executed?

(E) 8

What is d initialized to? double a = 9.2/0; int b = 10; double c = a*b; int d = a+c*2;

(E) A syntax error occurs.

A variable is defined as a storage location in memory that is composed of which of the following? I. a name II. a data type III. a value

(E) I, II, and III

Which of the following segments of code will swap the values stored in x and y, assuming that they are both positive, non-zero integers? For example, if x = 6 and y = 11, then after the code executes, the result should be that x = 11 and y = 6. I. x = x * y; y = x / y; x = x / y; II. int t = x; x = y; y = t; III. x = x + y; y = x - y; x = x - y;

(E) I, II, and III.

If int x = 6 and int y = 12, which of the following will evaluate to 0.5? I. (double) (x/y) II. (double) x / y III. x / (double) y

(E) II and III only

Which of the lines of code below contain syntax errors? Line 01: System.out.println("One); Line 02: System.out.println("Two"); Line 03: System.out.println("Three")

(E) Lines 01 and 03 only.

Assuming result is always a negative double, consider the following code segment which is intended to round result to the nearest int. int rounded = (int) (result + 0.5); System.out.println(rounded); Does the code segment work as intended?

(E) The code segment does not work properly because the formula for rounded should be (int) (result - 0.5).

Consider the following code segment. public static void main(String args[]){ int num1=9; int num2=10; System.out.println(" num1 is : "+ num1); num1++; System.out.println(" num1 is : "+ num1); num1%=num2; System.out.println(" num1 is : "+ num1); num2/=num1; System.out.println(" num2 is : "+ num2); } Which of the following best describes the behavior of the above code?

(E) The code will throw a "Divide by Zero" runtime error.

Knowing Integer.MIN_VALUE is equal to − 2^31 , consider the following code fragment. int y = Integer.MIN_VALUE; int x = -5; System.out.println(y - x); What is printed when the code segment is executed?

(E) The numerical equivalence to -2^31 + 5.

Which of the following segments of code will print 75.2 to the console?

(E) double num - 75.0; String str = "" + (num + 0.2); System.out.println(str);

Which of these identifiers is legal as a variable name in Java (i.e. it will compile successfully)?

(E) mySocialSecurity$_22


Conjuntos de estudio relacionados

Chapter 09: Maternal and Fetal Nutrition

View Set

DYGI Questions Chapter 9 Part 2: The Endocrine System

View Set

Chapter-11: Tactical Ventilatioin

View Set

Chapter 4: Validity and Test Development

View Set

Animal Development Quiz #2 Questions

View Set