Java 8 Questions for Oracle Certified Associate Java SE 8 Programmer 1

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What does a relationship expression do?

Compare two expressions and return a boolean value. i.e. x < y

During compilation, would will happen given the following statement? long x = 10; int y = 5; y = y * x;

Does not compile because "y" is promoted to a long and the result must then be a long data type.

What does the following code output? int x = 1; if (x = 5) { System.out.println("assigned to 5"); } System.out.println("Done");

Does not compile because (x = 5) is not a boolean operation.

What is the output from the following program: public class WaterBottle { private String brand; private boolean empty; public static void main(String[] args) { WaterBottle wb = new WaterBottle(); System.out.println("Empty = " + wb.empty()); System.out.println(", Brand = " + wb.brand(); } }

Empty = false, Brand = null You might think that the private access modifier for brand and empty would cause a compiler error, but it doesn't because main() is considered part of the class.

What does the following code do? boolean z = !0;

Fails to compile because 0 is an integer and the logical compliment operator can only be applied to a boolean.

What does the following code do? boolean y = -true;

Fails to compile because you can't set a boolean to negative

What is the scope of an instance variable?

In scope from the declaration until object garage collected.

How do you identify a class variable?

It has the keyword static before it.

In numeric promotion, what is the resulting data type

It will be the same data type as the promoted operand

What does the following code output: public class Finalizer { protected void finalize() { System.out.println("calling finalize"); } public static void main(String[] args) { Finalize f = new Finalizer(); } }

It won't output anything because the program exits before there is any need to run the garbage collector. For the exam, it is good to know that this finalize() method will run 0 or one time.

What is wrong with the following statement? double d1, double d2;

Java does not allow 2 types of variables to be declared even if they are of the same type on the same line.

In numeric promotion what happens when two values have different data types?

Java will automatically promote the one of the values to the larger of the two data types.

In numeric promotion, what happens when one value is an integral value and the other is a floating-point?

Java will promote the integral value to the floating values data type

When declaring a local variable, what is the default value?

Local variables do not have a default value and contain garage until initialized. You cannot use a local variable in a method until it is initialized.

When System.gc() is executed, is it guaranteed to run?

No

Will an object's finalize method always get called?

No

Will an object's finalize() method ever get called twice?

No

What are the 2 tests for Java developers provided by Oracle

OCA and OCP

What are the 6 key benefits to Java?

OO, E, PI, R, S, S 1.) object oriented 2.) encapsulation through access modifiers to protect data from unintended access. 3.) platform independent 4.) robust - no memory leaks. 5.) simple - no pointers, no operator overloading 6.) secure - runs in a JVM

What does OCA stand for

Oracle Certified Associate

What type of operators is the "instanceof" keyword an example of?

Relationship operator. Just like <, <=, >, >=, the "instanceof" returns a boolean value.

What does the following code output? int x = 1; if (x) { System.out.println(x); } System.out.println("done");

The code will not compile. x is not a boolean so it cannot be used like this.

Given the following package structure, which Water class will be used? <com.test.aquarium> class Water <jellies> class Water import com.test.aquarium.*; public class Main { public static void main(String[] args) { Water water = new Water(); } }

The com.test.aquarium.Water because the * imports the classes under aquarium, but not the other packages.

In the following snippet, what happens when the garbage collector run finalize()? public class Finalizer { private static List objects = new ArrayList(); protected void finalize() { objects.add(this); } }

The garbage collection of this object is aborted. Later, if the object becomes eligible, the finalize() method will not be called a second time.

Considering numeric promotion rules, if one value is integral and the other is a floating-point, what type of conversion will occur?

The integral value will be promoted to a floating-point value.

In numeric promotion, what happens when a byte, short or char are used?

They are promoted to int before being used in the calculation

What is wrong with the following statement? int num, String value;

This will not compile because you can't declare different variable types.

What is wrong with the following code snippet? public void test(boolean check) { int answer1; int answer2; if (check) { answer1 = 1; answer2 = 2; } else { answer2 = 2; } System.out.println(answer1); System.out.println(answer2); }

This won't compile because the local variable answer1 is never initialized.

What is the data type returned from the following expression? double x = 39.21; float y = 2.1; ? = x * y

Trick question - this fails to compile because a floating-point literal is assume to be double and since: float y = 2.1; is attempting to assign the double 2.1 to a float, it fails compilation.

During compilation, would will happen given the following statement? int x = 1.0;

Will not compile because 1.0 is assumed to be a double

During compilation, would will happen given the following statement? int z = 9f;

Will not compile because 9f is a floating-point value and requires a float data type.

During compilation, would will happen given the following statement? short x = 10; short y = 3; short z = x * y;

Will not compile because the short data type is automatically promoted to an int and the result will be an int which doesn't fit into "short z".

During compilation, would will happen given the following statement? short y = 1921222;

Will not compile because the value is too big

When declaring multiple variables on the same line what is the rule?

You can declare as many as you want, but they have to be of the same type and separated by commas. String s1="hello", s2="world;

Given the following class, which of the following is true? 01: public class Snake { 02: 03: public void shed(boolean time) { 04: 05: if (time) { 06: 07: } 08: System.out.println(result); 09: 10: } 11: } a.) if String result = "done"; is added to line 2, the code will compile b.) if String result = "done;"; is added on line 4, the code will compile c.) if String result = "done"; is added to line 6, the code will compile d.) if String result = "done"; is added to line 9, the code will compile e.) None of the above changes will make the code compile.

a is OK b is OK c won't work because result will be out of scope d won't work because it is too late e is obviously not the answer

Which of the following are OK? a.) short numPets = 5; b.) int numGrains = 5.6; c.) String name = "Scruffy"; d.) numPets.length(); e.) numGrains.length(); f.) name.length();

a is OK b is not because 5.6 is a float c is OK d is not because numPets is not an object e is not because numGrains is not an object f is OK because name is an object

Which of the following are valid Java identifiers? a.) A$B b.) _helloWorld c.) true d.) java.lang e.) Public f.) 1980_s

a, b, e c is a Java reserved word d is a Java package e is OK because public is the reserved word, not Public. f cannot start variables with a number

In a method declaration, the public, protected and private declarations are called what?

access modifiers

What are the 8 Java primitive types?

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

What does a .class file contain

bytecode

What is the generic term for packages that are defined after the com.company package?

child packages

Given the following class in the file /my/directory/named/A/Bird.java: INSERT CODE HERE public class Bird. Which of the following replaces INSERT CODE HERE if we compile from my/directory? Choose all that apply. a.) package my.directory.named.a; b.) package my.directory.named.A; c.) package named.a; d.) package named.A; e.) package a; f.) package A; g.) Does not compile

d.) package named.A

What does the following code do? int x = !5;

does not compile because the logical compliment operator cannot be applied to an integer.

If the value 2.1 is assigned to a data type, what type of data type would need to be used

double - because floating-point values with an "f" are automatically assumed to be doubles.

True or False - can you add a multiline comment within a multiline comment?

false

What is the default value for an instance variable of type boolean?

false

What is the output from the following snippet? File x = new File ("a.txt"); File y = new File ("a.txt"); File z = x; System.out.println(x == y); System.out.println(x == z);

false true This is because the equality comparison is done to the reference and not to what they point at.

What does the following program print? public class Test { public static void main(String[] args) { System.out.println(args[0]); } } java Test hello

hello

What is the scope of a class variable?

in scope from the declaration of the class until the program ends.

What is a code block outside of a method called?

instance initializer

What is the one special package in Java that you don't need to import?

java.lang

What package does the Files and Paths object belong to?

java.nio.file

What package is the ArrayList class in?

java.util.

What 2 common packages contain the Date class

java.util.Date and java.sql.Date

What command do you use to compile a Java class?

javac

What is the default value for an object reference?

null

What is the order of elements in a class?

package statements, import statements, class declaration, field declarations, method declarations. (PIC) Package/Import/Class.

What are the two types of data that Java applications contain?

primitive type and reference types

When defining a constructor for class Junk, what is the proper syntax?

public Junk() { } // notice there is NO return value.

Write the syntax for the main method.

public static void main(String[] args)

The full declaration of a method is called the method ____

signature

True or false, what creating a public Java class in a file does the case used in the file name have to match the case used by the Java class name?

true

What are the three types of operators available in Java?

unary, binary and ternary

What is the data type returned from the following? short x = 14; float y = 13; double z = 30; ? = x * y / z

x * y is performed first which promotes the x to an int and then promoted to a float because y is float. The result of x * y will be promoted to a double so that it can be divided by a double. ? will be a double

What is y? int x = 3; int y = ++x * 5 / x-- + --x;

y is 7 as follows: ++x * 5 / x-- + --x ++3 * 5 / x-- + --x 4 * 5 / 4 + 2 20 / 4 + 2 = 7

What is the logical compliment operator

!

What is an example of a condition operator?

&& and ||

What is the default value for an instance variable of type char?

'\u0000' (NUL)

If x and y are both boolean types, when will each of the following logical operator statements be true? if (x & y) if (x | y) if (x ^ y)

(x & y) - when x and y are both true. (x | y) - inclusive or, when either x or y are true. (x ^ y) - exclusive or - when x and y have different values. i.e. one has to true and the other false.

What are the 5 unary operators?

+ - ++ -- ! - which inverts a booleans logical value

What is an example of a unary operator?

++ or --

When you compile a Java class what file type extension is output?

.class

What is the default value for an instance variable of type byte, short, int and long?

0

What is the default value for an instance variable of type float or double?

0.0

How many public classes can be added to a single file

1

What is the result System.out.println(10 % 3)

1

What does the following code output? int x = 1; System.out.println(x++);

1 because the ++ is applied after the value is returned

What are the 3 rules for identifier naming?

1.) must begin with a letter or a $ or _ 2.) subsequent characters may also be numbers. 3.) cannot use Java reserved words.

What are the 3 styles of comments?

1.) single line comment 2.) multi-line comment 3.) JavaDoc comment

What year was the Java programming created

1995

What does the following code output? int y = 1; int z = 1; final int x = y<10 ? y++ : z++; System.out.println(y); System.out.println(z);

2 1 Ternary operators only execute the true side of the statement, not both. Be wary of any ternary expression in which a variable is modified in the right-hand side expression!

What does the following code output? int x = 1; System.out.println(++x);

2 because the ++ is applied before the value is returned.

What is the result: System.out.println( 10 / 3);

3

What will the following code output? long x = 5; long y = (x=3); System.out.println(x); System.out.println(y);

3 3 because the assignment x=3 is done. Note, this is a favorite type of question on the exam.

How many variables are declared and initialized in the following snippet? int i1, i2, i3=0;

3 are declared, 1 is initialized.

How many bits is an int primitive data type

32 bits

When creating an array, which one of these definitions is correct? 1.) String[] args, 2.) String args[], 3.) String... args, 4.) all of the above

4 All of the above

What is the output of the following snippet? int x = 6; boolean y = (x >= 6) || (++x <= 7); System.out.println(x);

6 because the (x >=6) is evaluated and the || short-circuit logic of the conditional operator takes over.

How many bits is a long primitive data type?

64 bits

What is the data type returned from the following expression? int x = 1; long y = 33; ? = x * y

? will be a long data type because of numeric promotion

What is the data type returned from the following expression? short x = 10; short y = 3; ? = x / y;

? will be an integer since shorts are automatically converted to ints when used in arithmetic operations.

If a class wanted to use the java.util.Date class, but the class had imports for java.util.* and java.sql.* which both contain the Date class how would you fix this?

Add an explicit import for java.util.Date since explicitly importing a class takes precedence over any wildcard imports.

Considering numeric promotion rules, if arithmetic operations are done using a byte, short, or char data type, what type of conversion is done?

All values are first promoted to int even if neither of the operands is an int.

In Java 7, what feature was added to make numbers easier to read?

An underscore option was created. int million = 1_000_000; the underscore cannot be first, before a decimal, after a decimal or at the end, but its OK elsewhere.

What is the range of data a Java byte can hold

Answer: -128 to 127

Given the class public class Junk { int eggs=0; public Junk() { eggs=1; } { eggs = 10; } } what is the value of eggs and why?

Answer: 1 Reason: The value is 1 because the fields and instance initializers are run in the order they are declared and lastly the constructor is run.

How many bits is a short data type?

Answer: 16 bits

How many bits is the char primitive type?

Answer: 16 bits

What is the maximum number an int can hold

Answer: 2,147,483,647

How many bits are used with the statement: int num;

Answer: 32

How many primitive data types does Java have?

Answer: 8 There are 8 primitive data types: boolean, byte, short, int, long, float, double and char.

How many bits is a byte primitive data type

Answer: 8 bits

What creating a JavaDoc statement. How do you define the description of method arguments?

Answer: @param argName <arg description> Reason: /** * @param inputArgument this is my input */

How would you define a literal hex value in Java?

Answer: Add a 0x prefix Reason: By adding a 0x prefix, you can define a hex number. For example; 0x1F is 31.

Is the following statement valid? int value = null;

Answer: No Reason: Primitive types cannot be set to null.

In the following code snippet, will this compile? public class Junk { { eggs = 6; } int eggs; } Why or why not?

Answer: Won't Compile Reason: This will not compile because the instance initializer will try to set a field that hasn't been declared yet.

As a general rule, all primitives start with: a.) lowercase b.) uppercase letters

Answer: a. lowercase Reason: That's just the way it is.

How would you define a literal octal number in Java?

Answer: add 0 prefix Reason: By adding a 0 prefix, you can define an octal number such as 017 which is equal to decimal 15.

How would you define a literal binary number in Java?

Answer: add the 0b prefix Reason: By adding 0b you can then add the binary values such as 0b11

Which of these floating point data types is bigger? float or double?

Answer: double Reason: A double type is larger because it is 64 bits and float is only 32.

When setting a float data type, what is the correct syntax?

Answer: float myfloat = 123.456f Reason: You have to remember to add the trailing "f".

When declaring a literal number what type does Java assume the data is?

Answer: int Reason: Java assumes the data is an int. If you want a really big number you have to add an "L" to the end to get a long.

Why are conditional operators sometimes called short-circuit operators?

Because the right side of the expression won't be evaluated if the if the final result can be determined by the left-hand side of the expression. i.e. Cat cat = null; if (cat != null) && (cat.paws() == 4) Since cat is null the cat.paws() is not executed.


Set pelajaran terkait

Civil War,Assassination of President Lincoln

View Set

Money and Banking Exam 2 Study Guide Ch. 10

View Set

JavaScript Practice Interview Questions

View Set

History of Rock MUS 28 Chapter 2

View Set

Chapter 15 HARVESTING THE ENTREPRENEURIAL VENTURE

View Set