OCA Java SE 7: Data Types

Ace your homework & exams now with Quizwiz!

What are the unary operators?

++ and --.

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

16 + the size of the initializing String

nWhat is the max int a char can hold?

65535

Give two example ways a String can be created.

A String object can be created by using the operator new , by using the assignment operator (=), or by using double quotes (as in System.out.println("Four")).

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.

Discuss what variable shadowing is and how to overcome it (unshadow).

A variable is shadowed if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope , the one in the outer scope is shadowed. For a local variable that shadows an instance variable, use the "this" keyword to access the instance variable. For a super class instance variable shadowed by a subclass, use the "super" keyword.`

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

Dereferenced

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 does the String method indexOf do?

It can be used to search a String for the occurrence of a char or a String, starting from the first position or a specified position.

How does the comparison operator (==) behave when applied to Strings?

It compares String references, whereas the equals method compares the String values.

Does the String class have a reverse():String method?

No, but StringBuilder does.

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 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.

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 is the rule for the expression result of two numeric types of int or smaller?

The result will always be an int: byte a = 1; byte b = 2; byte c = a + b // compiler error: result is an int. Need explicit cast to byte for this.

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.

What is a rule for identifying valid case labels?

The type of the switch variable must be capable of holding all switch labels.

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

True

True or False: None of the methods defined in the class String can modify its value.

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: The right technique for comparing two String values for equality is to use the method equals defined in the String class.

True. This method returns a true value if the object being compared isn't null and is a String object that represents the same sequence of characters as the object to which it's being compared. The comparison operator == determines whether both the reference variables are referring to the same String objects. Hence, it's not the right operator for comparing String values.

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.

Are the primitive wrapper objects (Integer, Long, etc.) immutable?

Yes.

Is this a valid expression: long myLong = 0x10L;

Yes. It is a long literal expressed in hex.

Will this method signature compile? void varArgMethod(int x, int... y)

Yes. A vararg parameter can be of the same type as another parameter, even if it is just the two of them.

What types can be used in a switch statement?

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

For what types is implicit narrowing on literals done?

byte, char, short, and int. So the following won't compile: int x = 120L;

What data types can store integer numbers?

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

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

integers or decimal numbers.

What are the eight Java primitive data types?

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

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.

Is goto a Java reserved keyword?

Yes.

Describe how the modulo operator "%" works for x%y.

x%y will return the remainder of x/y or x if x if y > x. E.g., 20%30 = 20 and 100 % 70 = 30.

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:

Categorize the primitive data types into three types.

Boolean, numeric, and character data types.

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.

True or False. A StringBuilder object can be created using its constructors, which can accept only a String object or nothing.

False. There are constructors that can accept either a String object, another StringBuilder object, an int value to specify the capacity of StringBuilder , or nothing.

True or False: The class StringBuilder is defined in the package java.lang and represents a mutable sequence of characters.

True.

True or False: The methods charAt, indexOf, substring, and length defined in the class StringBuilder work in the same way as methods with the same names defined in the class String.

True.

True or False: The substring method doesn't include the character at the end position.

True.

True or False: There is a String method length to retrieve the length of a String.

True.

True or False: You can use the concatenation operators + and += and comparison operators != and == with String objects. The Java language provides special support for concatenating String objects by using the operators + and +=.

True.

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: Unary operators can be used in prefix or postfix notation.

True.

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.

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 .

What data types are used to store decimal numbers?

float and double can be used to store decimal numbers.

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

int.

Which StringBuilder method overwrites characters within a string literal?

replace

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.

What does the String method charAt(int index) do?

It retrieves a character at a specified index of a String.

Does the String charAt(...) method accept a char or int argument?`

It will accept either of them since a char will implicitly be converted to an int. The method signature has an int parameter.

How does chaining of String methods work?

It's a common practice to use multiple String methods in a single line of code. When chained, the methods are evaluated from left to right.

Does the String charAt(..) method throw a StringIndexOutOfBoundsException?

No, it throws an IndexOutOfBoundsException.

Does the String charAt(...) method return a Character object?

No, returns a char primitive.

rDoes the StringBuilder class have a concat method?

No. It has an append(...) method. The String class does have a concat(String) method.

Discuss how String Pool of the JRE functions.

String objects created using the assignment operator are placed in a pool of String objects. Whenever the JRE receives a new request to create a String object using the assignment operator, it checks whether a String object with the same value already exists in the pool. If found, it returns the object reference for the existing String object from the pool. String objects created using the operator new are never placed in the pool of String objects.

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 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.

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 the String class have a concat(String):String method?

Yes

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

Yes

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.

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.


Related study sets

Chp 7 (PQ): Cellular Respiration & Fermentation

View Set

AP U.S History Colliding Cultures

View Set

Chapter 13 Return, Risk, and the Security Market Line

View Set

OSU CS361 Software Engineering I

View Set

Understanding Ultrasound Physics - SPI Exam Review - Period and Frequency

View Set