Chapter 2 Working with Java data types

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Exam Tip

Although questions 2-2 and 2-3 seemed to test you on your understanding of operators, they actually tested you on different topics. Question 2-2 tested you on the name of the primitive data types. Beware! The real exam has a lot of such questions. A question that may seem to test you on threads may actually be testing you on the use of a do-while loop!

Q2-3. What is the output of the following code? public class Foo { public static void main(String[] args) { int a = 10; long b = 20; short c = 30; System.out.println(++a + b++ * c); } } a) 611 b) 641 c) 930 d) 960

Answer: a Explanation: The prefix increment operator (++) used with the variable a will increment its value before it is used in the expression ++a + b++ * c. The postfix increment operator (++) used with the variable b will increment its value after its initial value is used in the expression ++a + b++ * c. Therefore, the expression ++a + b++ * c, evaluates with the following values: 11 + 20 * 30 Because the multiplication operator has a higher precedence than the addition operator, the values 20 and 30 are multiplied before the result is added to the value 11. The example expression evaluates as follows: (++a + b++ * c) = 11 + 20 * 30 = 11 + 600 = 611

Q2-6. Select the options that, when inserted at // INSERT CODE HERE, will make the following code output a value of 11: public class IncrementNum { public static void main(String[] args) { int ctr = 50; // INSERT CODE HERE System.out.println(ctr % 20); } } a) ctr += 1; b) ctr =+ 1; c) ++ctr; d) ctr = 1;

Answer: a, c Explanation: To output a value of 11, the value of the variable ctr should be 51 because 51%20 is 11. Operator % outputs the remainder from a division operation. The current value of the variable ctr is 50. It can be incremented by 1 using the correct assignment or increment operator. Option (b) is incorrect. Java does not define a =+ operator. The correct operator is +=. Option (d) is incorrect because it's assigning a value of 1 to the variable result, not incrementing it by 1.

Q2-2.Which of the options are correct for the following code? public class Prim { // line 1 public static void main(String[] args) { // line 2 char a = 'a'; // line 3 char b = -10; // line 4 char c = '1'; // line 5 integer d = 1000; // line 6 System.out.println(++a + b++ * c - d); // line 7 } // line 8 } // line 9 a) Code at line 4 fails to compile. b) Code at line 5 fails to compile. c) Code at line 6 fails to compile. d) Code at line 7 fails to compile.

Answer: a, c, d Explanation: Option (a) is correct. The code at line 4 fails to compile because you can't assign a negative value to a primitive char data type without casting. Option (c) is correct. There is no primitive data type with the name "integer." The valid data types are int and Integer (a wrapper class with I in uppercase). Option (d) is correct. The variable d remains undefined on line 7 because its declaration fails to compile on line 6. So the arithmetic expression (++a + b++ * c - d) that uses variable d fails to compile. There are no issues with using the variable c of the char data type in an arithmetic expression. The char data types are internally stored as unsigned integer values and can be used in arithmetic expressions.

Q2-4. Select the option(s) that is/are the best choice for the following: ___________________ should be used to store a count of cars manufactured by a car manufacturing company. _________________ should be used to store whether this car manufacturing company modifies the interiors on the customer's request. ____________ should be used to store the maximum speed of a car. a) long, boolean, double b) long, int, float c) char, int, double d) long, boolean, float

Answer: a, d Explanation: Options (a) and (d) are correct. Use a long data type to store big number values, a boolean data type to store yes/no values as true/false, and a double or float to store decimal numbers. Option (b) is incorrect. You can't use an int to store yes/no or true/false values. Option (c) is incorrect. You can't use a char data type to store very long values (such as the count of cars manufactured by the car manufacturer until a certain date). Also, it's conceptually incorrect to track counts using the char data type.

Q2-10. If the functionality of the operators = and > were to be swapped in Java (for the code on line numbers 4, 5, and 6), what would be the result of the following code? boolean myBool = false; // line 1 int yourInt = 10; // line 2 float hisFloat = 19.54f; // line 3 System.out.println(hisFloat > yourInt); // line 4 System.out.println(yourInt = 10); // line 5 System.out.println(myBool > false); // line 6 a) true true false b) 10.0 false false c) false false false d) Compilation error

Answer: b Explanation: Because the question mentioned swapping the functionality of the operator > with =, the code on lines 4, 5, and 6 will actually evaluate to the following: System.out.println(hisFloat = yourInt); System.out.println(yourInt > 10); System.out.println(myBool = false); The result is shown in b. Note that the expression myBool = false uses the assignment operator (=) and not a comparison operator (==). This expression assigns boolean literal false to myBool; it doesn't compare false with myBool. Watch out for similar (trick) assignments in the exam, which may seem to be comparing values.

Q2-9. Examine the following code and select the correct options: public class Prim { // line 1 public static void main(String[] args) { // line 2 int num1 = 12; // line 3 float num2 = 17.8f; // line 4 boolean eJavaResult = true; // line 5 boolean returnVal = num1 >= 12 && num2 < 4.567 // line 6 || eJavaResult == true; System.out.println(returnVal); // line 7 } // line 8 } // line 9 a) Code prints false b) Code prints true c) Code will print true if code on line 6 is modified to the following: boolean returnVal = (num1 >= 12 && num2 < 4.567) || eJavaResult ==true; d) Code will print true if code on line 6 is modified to the following: boolean returnVal = num1 >= 12 && (num2 < 4.567 || eJavaResult ==false);

Answer: b, c Explanation: Option (a) is incorrect because the code prints true. Option (d) is incorrect because the code prints false. Both the short-circuit operators && and || have the same operator precedence. In the absence of any parentheses, they are evaluated from left to right. The first expression, num1 >= 12, evaluates to true. The && operator evaluates the second operand only if the first evaluates to true. Because && returns true for its first operand, it evaluates the second operand, which is (num2 < 4.567 || eJavaResult == true). The second operand evaluates to true; hence the variable returnVal is assigned true.

Q2-1.Select all incorrect statements: a) A programmer can't define a new primitive data type. b) A programmer can define a new primitive data type. c) Once assigned, the value of a primitive can't be modified. d) A value can't be assigned to a primitive variable.

Answer: b, c, d Explanation: Only option (a) is a correct statement. Java primitive data types are predefined by the programming language. They can't be defined by a programmer.

Q2-5. Which of the following options contain correct code to declare and initialize variables to store whole numbers? a) bit a = 0; b) integer a2 = 7; c) long a3 = 0x10C; d) short a4 = 0512; e) double a5 = 10; f) byte a7 = -0; g) long a8 = 123456789;

Answer: c, d, f, g Explanation: Options (a) and (b) are incorrect. There are no primitive data types in Java with the names bit and integer. The correct names are byte and int. Option (c) is correct. It assigns a hexadecimal literal value to the variable a3. Option (d) is correct. It assigns an octal literal value to the variable a4. Option (e) is incorrect. It defines a variable of type double, which is used to store decimal numbers, not integers. Option (f) is correct. -0 is a valid literal value. Option (g) is correct. 123456789 is a valid integer literal value that can be assigned to a variable of type long.

Q2-7. What is the output of the following code? int a = 10; int b = 20; int c = (a * (b + 2)) - 10-4 * ((2*2) - 6; System.out.println(c); a) 218 b) 232 c) 246 d) Compilation error

Answer: d Explanation: First of all, whenever you answer any question that uses parentheses to override operator precedence, check whether the number of opening parentheses matches the number of closing parentheses. This code will not compile because the number of opening parentheses does not match the number of closing parentheses. Second, you may not have to answer complex expressions in the real exam. Whenever you see overly complex code, look for other possible issues in the code. Complex code may be used to distract your attention from the real issue.

Q2-8. What is true about the following lines of code? boolean b = false; int i = 90; System.out.println(i >= b); a) Code prints true b) Code prints false c) Code prints 90 >= false d) Compilation error

Answer: d Explanation: The code will fail to compile; hence, it can't execute. You can't compare incomparable types, such as a boolean value with a number.


संबंधित स्टडी सेट्स

Phylum: Cnidaria Classes: Hydrozoa, Scyphozoa, Anthozoa

View Set

Ch 5 - Residential Building Plans

View Set

AP Euro Schmitt Exam NOT FINISHED YET!!!

View Set

Army MICP - Tab A How the Assessment Was Conducted

View Set

Sociology - Real World - Ch 8: Race / Ethnic Group Issues

View Set

Chapter 11 - Interior Construction

View Set