Chapter 2: Build-in Data Types for Programming? Part 2
A Java String is a sequence of ______
chars A string is really just a sequence of char variables. It can contain any number of characters.
If you initialize a variable as currentTemperature, then how would you use the same variable in the System.out.prinln statement?
currentTemperature
In Java, the nextInt(int n) function generates a random number; it is a number between _____ and _______
0; a specified number (n). Zero; and a specified number. Zero; and a specified number. Zero; and a specified number. Zero; and a specified number. Zero; and a specified number.
The Java Boolean data type is a {Blank} data type?
Primitive Boolean data types, like int and float, are native to the programming language and can be used out of the box, without special coding.
Which of the following correctly imports only the FileFilter interface of the java.io API?
import Java.io.FileFilter;
You need to do some work with SQL and Java. You notice there is an API, java.sql. There are several classes you plan to use. What is the best way to import the API/APIs?
import Java.sql.*; Sometimes it can cause some issues with performance if you import an entire package (e.g., java.util). However, if you really are going to use most or all of the classes/interfaces, import all using the asterisk.
In the Java code bellow, what is the data type of the variable max? Random r = new Random(); int newR = r.nextInt(max); a. double b. float c. long d. int
int. int. int. int. int. int. int. int. The nextInt(int) function in the Random class takes an integer value.
If you need to see a long (longnum) to an int (intnum), which of the following would be used?
intnum=(int)longnum;
In Java, primitive data types are those that are ____, while non-primitive data types are ______
defined by the language, not defined by the language
What will the value of I be? int j = 5; int k = 50; int 1 = j - k;
-45 ust like in real math, you can subtract a larger number from a smaller one.
Examine the following code. What will the value of the variable tester be? int tester = 6 % 3;
0 The modulo operation will return a 0 if the division operation has no remainder.
What is the value of myNumber in the following code? int myNumber;
0 Remember that the default value for short, int, or long is 0.
A java char is ____ bits in size
16
Which of the following is an example of a floating point data type in Java? a. x = 4/0; b. x = 2.3; c. x = -0.1; d. x = -4.56899f;
x= -4.46899f;
Which of the following is a double in Java? a. x = 1.234567891234567; b. x = 0.123456789123456789123; c. x = 5f; d. x = 0. 0.12345f;
x= 1.234567891234567 A double in Java can store a maximum of 16 digits after the decimal. So if there are more than 16 digits, Java will throw and error. When f is appended to the decimal value the data is stored as a float, not a double.
Assume the binary representation of 42.75 is stored in a variable named myNumber that's declared as data type float. What numeric value will be stored in the variable theAnswer when the following Java statement is executed? float myNumber = 42.75f; byte theAnswer = (byte) myNumber;
42 When floating point data is converted to an integer, any fractional portion is discarded. Thus 42 will be stored in the 8-bit integer variable theAnswer.
Consider the following code/ What will the value of newChar be if disposed on the screen? char newChar = 65;
A Explanation Remember that the char data type stores the Unicode value. If you place the value of the variable in quotes, e.g., A, you will get A. However, by specifying a numeric value, you will get the value of that character. In this case, it is A.
The result of a Boolean operation is only true or false. How should this be written in Java code? a. Mix of uppercase and lowercase b. All lowercase c. Uppercase for false, lowercase for true d. All uppercase
All lowercase. All lowercase. All lowercase. All lowercase. The result of a Boolean operation is only true or false and should be written in lowercase in Java code.
The relation operators || and && (OR and ABD) are called ______ operators?
Boolean hey are called Boolean because they test for a true/false condition. However, all relational operators are basically used to check if a condition is true. Is the pay rate less than 50? If it is (true), then do some processing. Still, we call the AND and OR operators Boolean to separate them from the other operators.
Examine the following code. What is missing? if (orderID = 7) { // order ID = 7 }
Double-equal sign (==)instead of = this creates an assignment error: You aren't comparing but assigning 7 to the variable orderID. Most Java developer tools will not compile, but this can get you in big trouble in other languages/older tools. It can result in infinite loops or invalid/bogus results.
Examine the following code. What relational operators are bing used. if (currencyConversion == 0 || currentConversion > 100) { } 1. Equal to; OR 2. Equal to; OR; greater than 3. Equal to; greater than 4. Equal to; AND; greater than
Equal to; OR;greater than == Equal to; ll is or; > is for greater than.
Java won't ______ convert the char data type to a numeric data type (even if the target data type is wilder), but a programmer can explicitly use a ___ to force the conversion. 1. automatically, comment 2. implicitly, comment 3. implicitly, type cast 4. automatically, declaration
Implicitly, type cast. Implicitly and type cast. Implicitly and type cast. Implicitly and type cast. Implicitly and type cast. Java won't implicitly (automatically) convert the char data type to a numeric data type (even if the target data type is wider), but a programmer can explicitly use a type cast (such as '(int)') to force the conversion
What is the result of x = 5.0/ 0.0 in Java? a. 0 b. Error c. Infinity d. 0.0
Infinity. Infinity. Infinity. Infinity. Infinity. Infinity. In Java you can divide a double by 0.0 and the result will be Infinity.
The char data type is most closely related to the _____ data type
Integer (int)
Which data types are NOT an example of a primitive data type? 1. char 2. boolean 3. interface 4. int
Interface. Interface. Interface. Interface. Interface. Interface. Interface. Interface is a non-primitive data type, along with class and array.
The values allowed for the boolean type are true and false, and they are______
Keywords They are keywords; reserved by Java. That means they cannot be used for variable, object, class, or function names.
n the following code, what relational operator is being used? double currencyConversion; if (currencyConversion <= .00035) { //code here }
Less than or equal to The code is checking to see if the currency conversion is less than or equal to .00035
The Java Random class has a function that lets you generate a ___________number?
Pseudorandom
If you declare a char variable and assign a numeric value to it, which of the following will display?
The corresponding Unicode representation of that value.
Examine the following code. The programmer is not getting the same value for myNumber and myNumber2. Why? double myNumber = (20 - 10 / 3); double myNumber2 = ((20 - 10 / 3);
The order of operations is different in each. Java math works like real math. If you have a different order of operations, such as placing parentheses in different positions, results will be very different!
Why is the word 'final' used to describe a content in Java?
The value of the content cannot change through the execution of the program
What are floating point data types in Java used for?
To store decimal numbers Floating point numbers or literals are used to store decimal values in Java.
To move the contents of a variable declared as data type floatinto a variable declared as data type long requires the use of _____.
a type cast
Java contents are usually names with upper case letters with an underscore between worlds. However, which of the following would also compile as the name for a content?
ifElse
Consider a program that calculates distances between stars. What data type would be best suited for the variable that stores this number (e.g.,distance)?
long The distance is a very large number, and can be displayed in whole numbers. However, byte, int and short are way too small to hold such large values.
What are the two data types used to store floating point literals in Java?
Float and Double For calculations involving decimal numbers, Java supports two data types, Float and Double.
Will the following code work?
char myChar = 'ABC'; No, you can only store one literal character in a char.
Java will _______ move a value stored in a short data type into a variable declared as int
Implicitly Java will implicitly (i.e., automatically) move the value from a 16-bit short into a 32-bit int because there's no possible loss of information moving into a wider data type.
What is the size of char data type?
16-bit
What is missing from the following Java code? int v1 = min + r.nextInt(); a. An integer for the upper value b. An integer for the lower value c. An instance of the Random class d. Integer variable v1 is invalid
An integer for the upper value.
A Java string is both an. object and a ______
Class
A java String is a_______
Class
What kind of data types will you use to for an object 'Pen'?
Class A class is a user-defined data type. Hence 'class' is the correct answer.
Which of the following is included in a Java API?
Classes and Methods An API is a package: Included within the package are interfaces and classes, each with methods, fields and constructors.
Which of the following is true about constant naming conventions on Java?
Constants should be upper case with an underscore separating the words
What are these two symbols for: / and %
Division and Modulo The first symbol, the forward slash, is used for division. The second symbol is the modulo operator.
When multiplying and/or dividing large, complex numbers, which data type is best
Double
Examine the following code. After it completes, what will the value be for the boolean variable isExpired boolean isExpired = true; int myValue = 5; if (myValue < 10) { isExpired = false; } a. 0 b. False c. Undefined d. True
False. False. False. False. False. False. False. False. Although the value was set to true, it's only going to be set to false if the value of myValue is less than 10. Since myValue is 5, the boolean value will be flipped to false
Examine the following code. What is the default value of the variable validEntry? boolean validEntry; a. False b. True c. 0 d. No value
False. False. False. False. False. False. False. False. ava cannot read your mind when it comes to boolean variable declarations. That said, the default for the boolean primitive type is FALSE. If you do not declare your variables with an initial value, it could give you headaches later!
Based on the Java code below, what is true about the variable r? int rNew = r.nextInt(5); a. It is a float. b. It is an instance of the Random class. c. It will be null. d. It has a value of 5.
It is an instance of the Random class. Even though we don't have the full code, we can assume r would be an instance of the Random class because it invokes the nextInt() function.
Examine the following code. What will the value of the variable str3 be?
JOIN US AT 6! The toUpperCase() function converts all of the characters to uppercase. The exclamation point is kept intact as it has no uppercase counterpart.
The following code is displaying an error. Stating that it cannot find the symbol (class: File). What is wrong? File writer = new File(''//users/mdg/myfile.txt'');
Need to import the file API Either import java.io.* or java.io.File; You can instantiate an instance of the File class, but you need to make sure to import the API.
Java ______ allows boolean true/false values to be converted to/from other data type 1. always 2. never 3. explicitly 4. implicitly
Never. Never. Never. Never. Never. Never. Java PROHIBITS boolean true/false values from being converted to/from ANY other data type.
Examine the following code. What relational operator is being used? double currencyConversion; while (currencyConversion != 1000) { // code here }
Not equal to
Java Application Programming Interface (API) is a list of ______
Packages At the top level, each API is really a Java package. Each package has its own interfaces, fields, methods, and constructors. For example, the general util (utility) package has a class called Scanner, which lets you take input from the keyboar
The data types short int, and long are all?
Whole integers They are all whole numbers, either negative or positive, with a set range of values. No decimals are allowed.
Which data type is 1-bit in size? 1. boolean 2. short 3. long 4. char
boolean. boolean. boolean. boolean. Data type boolean is 1-bit in size and it holds value true or false. The default is false.
Which of the following is the correct way to declare a variable in Java? a. int begin_Time = 0; b. final int beginTime = 0; c. final String beginTime = 0; d. int begin Time = 0;
int begin_Time = 0;
Which of the following is a valid declaration of an int data type?
int myNumber = 532524584; The range of an int is -2,147,483,648 to 2,147,483,647, and only allows whole, integer values.