












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













What year was the Java programming created correct answers What does OCA stand for correct answersOracle Certified Associate What are the 2 tests for Java developers provided by Oracle correct answersOCA and OCP The full declaration of a method is called the method ____ correct answerssignature True or False - can you add a multiline comment within a multiline comment? correct answersfalse How many public classes can be added to a single file correct answers Write the syntax for the main method. correct answerspublic static void main(String[] args) When you compile a Java class what file type extension is output? correct answers.class What does a .class file contain correct answersbytecode 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? correct answerstrue In a method declaration, the public, protected and private declarations are called what? correct answersaccess modifiers What is the one special package in Java that you don't need to import? correct answersjava.lang
What is the generic term for packages that are defined after the com.company package? correct answerschild packages 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 correct answers All of the above What package does the Files and Paths object belong to? correct answersjava.nio.file What 2 common packages contain the Date class correct answersjava.util.Date and java.sql.Date 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? correct answersAdd an explicit import for java.util.Date since explicitly importing a class takes precedence over any wildcard imports. What command do you use to compile a Java class? correct answersjavac What package is the ArrayList class in? correct answersjava.util. When defining a constructor for class Junk, what is the proper syntax? correct answerspublic Junk() { } // notice there is NO return value. What is a code block outside of a method called? correct answersinstance initializer Given the class public class Junk { int eggs=0; public Junk() { eggs=1; } { eggs = 10; } } what is the value of eggs and why? correct answersAnswer: 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 byte primitive data type correct answersAnswer: 8 bits How many bits is a short data type? correct answersAnswer: 16 bits When setting a float data type, what is the correct syntax? correct answersAnswer: float myfloat = 123.456f Reason: You have to remember to add the trailing "f". What is the range of data a Java byte can hold correct answersAnswer: -128 to 127 How many bits are used with the statement: int num; correct answersAnswer: 32 What is the maximum number an int can hold correct answersAnswer: 2,147,483, When declaring a literal number what type does Java assume the data is? correct answersAnswer: 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. How would you define a literal binary number in Java? correct answersAnswer: add the 0b prefix Reason: By adding 0b you can then add the binary values such as 0b How would you define a literal octal number in Java? correct answersAnswer: 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 hex value in Java? correct answersAnswer: Add a 0x prefix Reason: By adding a 0x prefix, you can define a hex number. For example; 0x1F is 31. In Java 7, what feature was added to make numbers easier to read? correct answersAn 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. Is the following statement valid? int value = null; correct answersAnswer: No Reason: Primitive types cannot be set to null. As a general rule, all primitives start with: a.) lowercase b.) uppercase letters correct answersAnswer: a. lowercase Reason: That's just the way it is. When declaring multiple variables on the same line what is the rule? correct answersYou 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; How many variables are declared and initialized in the following snippet? int i1, i2, i3=0; correct answers3 are declared, 1 is initialized.
System.out.println(answer1); System.out.println(answer2); } correct answersThis won't compile because the local variable answer1 is never initialized. How do you identify a class variable? correct answersIt has the keyword static before it. What is the default value for an instance variable of type boolean? correct answersfalse What is the default value for an instance variable of type byte, short, int and long? correct answers What is the default value for an instance variable of type float or double? correct answers0. What is the default value for an instance variable of type char? correct answers'\u0000' (NUL) What is the default value for an object reference? correct answersnull What is the scope of a class variable? correct answersin scope from the declaration of the class until the program ends. What is the scope of an instance variable? correct answersIn scope from the declaration until object garage collected. What is the order of elements in a class? correct answerspackage statements, import statements, class declaration, field declarations, method declarations. (PIC) Package/Import/Class. When System.gc() is executed, is it guaranteed to run? correct answersNo Will an object's finalize method always get called? correct answersNo Will an object's finalize() method ever get called twice? correct answersNo
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(); } } correct answersIt 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. 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); } } correct answersThe garbage collection of this object is aborted. Later, if the object becomes eligible, the finalize() method will not be called a second time. What are the 3 styles of comments? correct answers1.) single line comment 2.) multi-line comment 3.) JavaDoc comment What are the 6 key benefits to Java? correct answersOO, E, PI, R, S, S
f cannot start variables with a number 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(); } } correct answersEmpty = 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. 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(); correct answersa 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
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. correct answersa 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 Given the following package structure, which Water class will be used? <com.test.aquarium> class Water
What is an example of a unary operator? correct answers++ or -- What is the result: System.out.println( 10 / 3); correct answers What is the result System.out.println(10 % 3) correct answers In numeric promotion what happens when two values have different data types? correct answersJava 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? correct answersJava will promote the integral value to the floating values data type In numeric promotion, what happens when a byte, short or char are used? correct answersThey are promoted to int before being used in the calculation In numeric promotion, what is the resulting data type correct answersIt will be the same data type as the promoted operand What is the data type returned from the following expression? int x = 1; long y = 33; ? = x * y correct answers? will be a long data type because of numeric promotion What is the data type returned from the following expression?
double x = 39.21; float y = 2.1; ? = x * y correct answersTrick 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. What is the data type returned from the following expression? short x = 10; short y = 3; ? = x / y; correct answers? will be an integer since shorts are automatically converted to ints when used in arithmetic operations. What is the data type returned from the following? short x = 14; float y = 13; double z = 30; ? = x * y / z correct answersx * 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 are the 5 unary operators? correct answers+
What is y? int x = 3; int y = ++x * 5 / x-- + --x; correct answersy is 7 as follows: ++x * 5 / x-- + --x ++3 * 5 / x-- + --x 4 * 5 / 4 + 2 20 / 4 + 2 = 7 What creating a JavaDoc statement. How do you define the description of method arguments? correct answersAnswer: @param argName
During compilation, would will happen given the following statement? int x = 1.0; correct answersWill not compile because 1.0 is assumed to be a double During compilation, would will happen given the following statement? short y = 1921222; correct answersWill not compile because the value is too big During compilation, would will happen given the following statement? int z = 9f; correct answersWill 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; correct answersWill 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? long x = 10; int y = 5; y = y * x; correct answersDoes not compile because "y" is promoted to a long and the result must then be a long data type. What will the following code output?
i.e. Cat cat = null; if (cat != null) && (cat.paws() == 4) Since cat is null the cat.paws() is not executed. What is the output of the following snippet? int x = 6; boolean y = (x >= 6) || (++x <= 7); System.out.println(x); correct answers because the (x >=6) is evaluated and the || short-circuit logic of the conditional operator takes over. 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); correct answersfalse true This is because the equality comparison is done to the reference and not to what they point at. What does the following code output? int x = 1; if (x) {
System.out.println(x); } System.out.println("done"); correct answersThe code will not compile. x is not a boolean so it cannot be used like this. What does the following code output? int x = 1; if (x = 5) { System.out.println("assigned to 5"); } System.out.println("Done"); correct answersDoes not compile because (x = 5) is not a boolean operation. 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); correct answers 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!