Java SE 7: Data Types -Lmay

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is the max int a char can hold?

65535

Categorize the primitive data types into three types.

Boolean, numeric, and character data types.

In what phase is an object when it goes out of scope and has no remaining live pointers?

Dereferenced

If two reference variables are a base type but are assigned base and subclass objects, which overridden static methods will be called? Those of the base or subclass? E.g., Animal and Cat?

Explanation: Invocation of a static method is tied to the type of the reference variable and doesn't depend on the type of the object that's assigned to the reference variable. So if the references both are of type Animal, then Animal's static method will be called and not Cat's.

What are the relational operators?

Relational operators are used to compare values for equality (==) and non-equality (!=). They are also used to determine whether two numeric values are greater than (>, >=) or less than (<, <=) each other. The operators equal to (==) and not equal to (!=) can be used to compare all types of primitives: char , byte, short, int, long, float, double, and boolean. The operator == returns true if the primitive values being compared are equal. The operator != returns true if the primitive values being compared are not equal. The result of the relational operator is always a boolean value.

How to designate a decimal literal value as a double value?

The suffixes D and d can be used to mark a literal value as a double value.

True or False: Primitive data types are predefined by the programming language. A user can't define a primitive data type in Java.

True

Is the "+" sign in front of numeric literals accepted by the compiler?

Yes

What are the range of unicode values for a char?

You can use values from \u0000 (or 0) to a maximum of \uffff (or 65,535 inclusive) to store a char .

How to designate a decimal literal value as a float value?

add the suffix F or f to the literal value.

What suffix is used to designate an integer literal value as a long value?

add the suffix L or l to the literal value

What is the default type of a decimal number?

double.

Which StringBuilder method overwrites characters within a string literal?

replace

What is the capacity of a StringBuilder if a String is specified in its constructor?

16 + the size of the initializing String

What are the unary operators?

++ and --.

What are the bit sizes of the types used to store integer numbers?

The byte, short, int, and long data types use 8, 16, 32, and 64 bits, respectively, to store their values.

True or False:The class String represents an immutable sequence of characters.

True.

True or false: A variable can't be assigned to an incompatible value. For example, character and numeric values cannot be assigned to a boolean variable, and vice versa.

True.

True or false: By default, unary operators have a higher precedence than multiplication operators and addition operators.

True.

True or false: Single quotes, not double quotes, are used to assign a letter to a char variable.

True.

True or false: Unary operators can be used in prefix or postfix notation.

True.

What is a literal value?

A literal is a fixed value that doesn't need further calculations to be assigned to any variable. Numeric data types:

What data types are used to store decimal numbers?

float and double can be used to store decimal numbers.

What types of values can numeric values can be stored as?

integers or decimal numbers.

What data types can store integer numbers?

byte, short, int, and long can be used to store integers.

How many bits does the char data type have?

A char data type can store a single 16-bit Unicode character; that is, it can store characters from virtually all the world's existing scripts and languages.

In what phase is an object when the garbage collector is about to remove it from memory?

Finalized

Discuss why the logical operators && and || are also called short-circuit operators.

If these operators can determine the output of the expression with the evaluation of the first operand, they don't evaluate the second operand. The && operator returns true only if both of the operands are true. If the first operand to this operator evaluates to false, the result can never be true. Therefore, && does not evaluate the second operand. Similarly, the || operator returns true if any of the operands is true. If the first operand to this operator evaluates to true, the result can never be false. Therefore, || does not evaluate the second operator.

What are the eight Java primitive data types?

char , byte, short, int, long, float, double, and boolean.

Is 314_15.23_D a valid numerical literal in Java 7?

No. The underscore is contiguous to the D suffix.

Discuss when implicit and explicit narrowing/widening is done at compile time for primitives. Use statements below for discussion. byte by = 128; byte by1 = 127; char c = 256; int idx = 2; c = idx; short s = idx; byte b = (byte) idx; byte by2 = '\u0000'; byte by3 = '\uffff'; byte by4 = 'a'; long l = idx; l = 256; double d = 256;

// compile error, literal out of range byte by = 128; // Implicit narrowing cast of literals byte by1 = 127; char c = 256; int idx = 2; // Narrowing assignment via type references requires a cast c = idx; short s = idx; byte b = (byte) idx; // Implicit narrowing not possible, literal out of range byte by3 = '\uffff'; // Implicit narrowing ok here, in range byte by2 = '\u0000'; byte by4 = 'a'; // Implicit widening done long l = idx; l = 256; double d = 256;

What are the rules for a valid variable identifier:

A valid identifier starts with a letter (a-z, upper- or lowercase), a currency sign, or an underscore. There is no limit to its length. A valid identifier can contain digits, but not in the starting place. A valid identifier can use the underscore and currency sign at any position of the identifier. A valid identifier can't have the same spelling as a Java keyword, such as switch. A valid identifier can't use any special characters, including !, @, #, %, ^, &, *, (,), ', :, ;, [, /, \, or }

Which three locations are invalid when placing underscores in a numeric literal?

Beginning or end of a number, contiguous to decimal points, or contiguous to D, F, and L suffixes.

True or False: the declaration is valid, float myFloat = 3.15

False. The default literal type for decimal values is double. Assigning a double to a float variable would result in a loss of precision. The declaration would result in a compiler error. The declaration could be corrected by either casting the literal to a float or using the f or F postfix notation appended to the literal.

Will this code compile? public class Application { public static void main(String... args) { String model; System.out.println(model); } }

No. Explanation: The local variables aren't initialized with default values. Code that tries to print the value of an uninitialized local variable fails to compile.

What values can the boolean data type accept?

The boolean data type is used to store data with only two possible values. These two possible values may be thought of as yes/no, 0/1, true/false, or any other combination. The actual values that a boolean can store are true and false.

What are the bit sizes of the data types used to store decimal numbers?

The float and double data types use 32 and 64 bits, respectively, to store their values.

What are the prefixes for the four numeric formats?

The literal values in the octal number system start with the prefix 0. For example, 0413 in the octal number system is 267 in the decimal number system. The literal values in the hexadecimal number system start with the prefix 0x. For example, 0x10B in the hexadecimal number system is 267 in the decimal number system. The literal values in the binary number system start with the prefix 0b or 0B. For example, the decimal value 267 is 0B100001011 in the binary system.

True or false: Unicode values are defined in the hexadecimal number system.

True. Internally, the char data type is stored as an unsigned integer value (only positive integers).

Does this class compile: class Emp { Emp mgr = new Emp(); }

Yes, However, when the default constructor is called, the member variable will be initialized and cause a recursive call to the default constructor. The code throws java.lang.StackOverflowError at runtime.

Does the following line compile: char ch = 10;

Yes, and int literal will fit into a char.

Will the primitive wrapper classes accept primitives in their overridden equals method?

Yes. An Integer object and accept a primitive int as an argument to its equals method.

What are the logical operators?

You can use the logical operators to determine whether a set of conditions is true or false and proceed accordingly. Logical AND ( && ) evaluates to true if all operands are true, and false otherwise. Logical OR (||) evaluates to true if any or all of the operands is true. Logical negation (!) negates the boolean value. It evaluates to true for false, and vice versa. The result of a logical operation is always a boolean value.

Starting with Java 7, a new format was made available for numeric literal variables. Describe it.

You can use underscores within the Java literal values to make them more readable. 0B1_0000_10_11, 0_413, and 0x10_B are valid binary, octal, and hexadecimal literal values.

Which StringBuilder method adds to the end of a string literal?

append

What are the four numeric formats?

binary, octal, decimal, and hexadecimal number formats.

What types can be used in a switch statement?

byte, char, short, int, enum, and String.

What is the default type for integers—that is, nondecimal numbers?

int.


Ensembles d'études connexes

Critical Care Silvestri Questions Exam 1

View Set

Chapter 3: Title VII of the Civil Rights Act of 1964

View Set

chapter 12 us HIST true false practice quiz

View Set

Org. Theory and Behavior Final Exam Study Guide

View Set