Lesson 1: Introduction to Java

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

Which of the following is not a primitive type? A) String B) byte C) char D) double

A. byte, char, short, int, long, float, double, and boolean are all primitive types. While Strings are commonly used, they are not primitive.

True or False: In Java, ints can be cast to doubles without any loss of precision, and vice versa. A) True B) False

B. When you cast a double to an int, the decimal part is truncated and simply not included in the resulting int. The reverse operation is a widening conversion, so there's no loss of data.

Which of the following statements are true? You may select more than one, but you must select at least one. A) A program that does not use whitepsace to indent lines within a method or class may still compile and run. B) There are 3 types of errors: compiler errors, interpreter errors, and logical errors. C) Semantics refers to the rules of a particular coding language that you must follow when writing code in that language; syntax refers to the meaning of the code. D) Syntactically correct code will always be semantically correct.

A

Which of the following statements are true? You may select more than one, but you must select at least one. A) Methods allow for grouping statements together in a Java program. B) All Java programs must have at least one method and at least one class. C) Class names may have spaces in them. D) When saving Java source code, you must use the ".class" extension. E) Interpreters translate programs from a high-level language to a low-level language by generating new files. F) Compiled languages are generally faster than interpreted languages. G) Machine code generated by a compiler on one computer can always be used to run the program on another computer.

A, B, F

Which of the following statements are true? You may select more than one, but you must select at least one. A) An object variable stores a reference to the location in memory where the object is stored. B) Instantiation refers to assigning a value to a primitive type variable. C) The constructor initializes certain properties of a new object using provided parameters. D) The keyword "instance" is used to create a new instance of a certain class. E) The keyword "new" must be used to create a new instance of the String class.

A, C

Which of these are illegal Java identifiers for an int variable that will store a temperature value? A) why? B) DID_THE C) chicken D) cro55 E) 5street

A, E. Identifiers can only consist of letters, digits, _, and $. They cannot be reserved words, and they cannot start with a digit.

Forgetting to declare a variable in Java produces an error during: A) Compile Time B) Runtime

A. Java is statically typed, so each variable must be explicitly declared with a type before using it. The compiler checks for a type for each variable and generates an error if none has been declared. Not declaring a variable is one way to generate a "cannot find symbol" error during compilation.

Which of the following generates bytecode for the HelloWorld program? A) javac HelloWorld.java B) java HelloWorld.java C) javac HelloWorld D) java HelloWorld

A. The javac command initiates the Java compiler, which generates bytecode in the form of a .class file.

Which of the following statements are true? You may select more than one, but you must select at least one. A) In a class, methods represent the state of an object, and variables represent that object's behavior. B) Calling a method can change the values of an object's variables. C) State variables within objects created from the same class will have the same values. D) After changing a program, you must recompile it before running it if you want to see your changes reflected in the output.

B, D

True or False: In Java, 3/4 = 0.75. A) True B) False

B. 3/4 is an example of integer division, so the end result must be an integer value after the decimal part is discarded. In this case, the result is simply 0.

Consider the code below. String playerOne = "Alex"; String playerTwo = "Kathy"; playerTwo = playerOne; int lengthOne = playerOne.length(); int lengthTwo = playerTwo.length(); System.out.println(lengthOne + " " + lengthTwo); What will this code print to the console? A) 4 5 B) 4 4 C) 5 5 D) 4 0

B. playerOne initially refers to the string "Alex", and playerTwo initially refers to the string "Kathy". Then, playerTwo is changed to refer to the same object as playerOne. Both variables now refer to the string "Alex", so they will both have a length of 4.

Consider the code below. int numPizzas = 2; System.out.println("She said, \"I would like to order " + numPizzas + " large pizzas, please.\""); What will this code print to the console? A) Nothing will be printed, and a compiler error will result from trying to add two incompatible data types (int and string). B) She said, \"I would like to order 2 large pizzas, please.\" C) She said, "I would like to order 2 large pizzas, please." D) She said, "I would like to order numPizzas large pizzas, please."

C

Select the primitive type of this expression's result: 32.0 % 6 A) int B) float C) double D) long

C. The 6 is promoted from an int to a double, which then leads to a double result.

Consider the code below. int x = 4; int y = 6; int z = 12; double a = (double)z/x; double b = y; System.out.println(a + " " + b); What will this code print to the console? A) 3.0 6 B) 3 6.0 C) Nothing will be printed, and a lossy conversion compiler error will result from trying to assign an int to a double. D) 3.0 6.0

D

True or False: You must initialize a variable before you declare it.

False. Declaring a variable allocates a location in memory to store a value. Therefore, initializing the variable with a value cannot happen before there's a location to hold the value.

True or False: The Java Virtual Machine (JVM) generates bytecode.

False. During runtime, the JVM interprets bytecode, which is generated by the compiler.

True or False: Interpreters translate programs from a high-level language to a low-level language by generating new files.

False. Interpreters translate programs line by line on the fly without generating new files.

True or False: Compiled languages only perform translation at compile time.

False. Java is a compiled language; however, translation happens during compilation and runtime due to its hybrid approach.

True or False: Java variables can be used in expressions without being declared.

False. Java variables must be declared before they can be used anywhere in the code.

True or False: Java was the first widely used Object-Oriented Programming language.

False. Other popular Object-Oriented languages existed before Java. C++, for example, appeared almost a decade before Java was released.

True or False: The command javac invokes the Java interpreter.

False. The "c" in "javac" represents the term "compiler". The interpreter (or virtual machine) is invoked by the "java" command.

True or False: Between byte and short, byte is the primitive type that allows you to store the lowest numeric value.

False. short's lowest value is -32,768, while byte's is -128.

True or False: In Java, it is conventional to use camel casing (capitalizing the first letter of each word) in variable names, with the first letter of the name in lowercase.

True.


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

Sociology Final Review (Tests 1-3)

View Set

Chapter 4 - Reproductive system concerns (EAQ's)

View Set

Management Chapter 4- Dr. Loes (Belmont University)

View Set

Geriatrics Chapter 33 and 38 Foundation

View Set