Shared Flashcard Set

Details

Java Cert - OCAJ-SE8 - Chapter 1 - Java Building Blocks
Flashcards for fundamental Java - Variables, Classes, etc
94
Software
Professional
12/27/2017

Additional Software Flashcards

 


 

Cards

Term
What are the members of a Class?
Definition
Fields and Methods
Term
If a class is public and has multiple classes in the file, what is the requirement for this scenario?
Definition
That the class in the file that is public is the same name as the file.
Term
When you compile a .java file, what is it turned into?
Definition
It is turned into a .class file which contains bytecode that the JVM can read and run.
Term
When you call a program and one of the arguments has a space in it, how do you enter it on the command line to make sure it gets into the main method as expected?
Definition

You wrap it in quotes "".

 

java Zoo "Bronx Zoo" Zebra (has two arguments)

Term
What does it mean if you only have a JRE installed on a machine, and not a JDK?
Definition
It means that you can only run programs on that machine but that you cannot not compile .java files.
Term
How do you import all the classes in a package?
Definition

use wildcard

java.util.*;

Term
Which package in Java is automatically imported and what is one of the most important classes?
Definition

java.lang

java.lang.System

Term
What are some rules about wildcard characters in import statements?
Definition

The wildcard must be the last character in a package that has classes.

There must only be one wildcard char in the import statement.

You cannot use wildcards to import members (except when using static import).

Term
What happens when you have multiple import statements that contain a class you need that has the same name in multiple imports?
Definition
It will not compile.  You need to use an explicit import for the class you need and not a wildcard.
Term

True or false - If you explicitly import a class name, that one takes precedence over another found in an import statement using a wildcard.

 

Import java.util.Date;

import java.sql.*;

Definition
True
Term
What happens if you have two explicit import statements that have classes with the same name?
Definition
It will not compile and will generate an exception that one "collides with the other import statement."
Term
What happens when you do not use a package statement for your class?
Definition
The class is placed in the default package, which is a special unnamed package where compiled classes appear in the root of the compiled classes folder in the project.
Term

If you run a java program with the -cp instruction argument, and the path has a wildcard char in it, what does this do?

 

java -cp "C:\temp\directoryWithJars\*" myPackage.MyClass

Definition
It will look for the main class in all the jars in the folder with the wildcard. It will NOT look in any subdirectories underneath the wildcard character in the -cp argument.
Term
What keyword is used to create an instance of a class?
Definition
new
Term
what is the special type of method used in a class that will create an instance of it?
Definition
constructor
Term
What are the two key components of a constructor?
Definition

Must have the exact same name as the class.

Cannot have a return value.

Term
What happens if you don't include any constructor in your class?
Definition
The compiler will insert a no-arg default constructor.
Term
What is the difference between static and instance initializers?
Definition

Static initializer or Static block

Static {

 

}

 

Instance Initializer or Instance block

 

{

 

}

Term
What is a general rule of order of initialization?
Definition
Constructors run AFTER static field and blocks, then instance fields and blocks have been initialized.
Term
True or false - float does not require the use of "f" appended to the end of a decimal number.
Definition
False, Java does require this so it knows that it is a float, other wise it would assume it is a double.
Term
Show the bits taken up by each numeric primitive data type.
Definition

Non-decimal

byte = 8 bit

short = 16 bit

int = 32 bit

long = 64

decimal

float = 32 bit

double = 64 bit

character = 16 bit unicode

Term
What is the range of values a byte can have?
Definition
-128 thru 127
Term
True or false - If you have a non-decimal number in the code, it is called a literal and Java assumes it to be an int.
Definition
True
Term
If you have a number that is too big too fit in an int, you can put it in a long but you must remember to include what?
Definition
You must append an "L" to the end of the number so Java knows it is a long.
Term
If you have a number that is too big too fit in an int, you can put it in a long but you must remember to include what?
Definition
You must append an "L" to the end of the number so Java knows it is a long.
Term
Which numeric format uses a range of digits from 0-7, and allows you to prefix a value with a zero (0).
Definition
Octal based
Term
Which numeric format uses digits 0-9, and the letters A-F, and uses 0 and x or X as a prefix.
Definition
Hexidecimal
Term
Which numeric format uses digits 0-1, and uses 0 and b/B as a prefix?
Definition
Binary
Term
What are the rules for using underscores _ in numeric values.
Definition

Cannot be a the beginning or the end.

Can be anywhere in the middle of non decimal numbers.

In numbers with decimals, cannot be next to on the left or next to on the right of the decimal.

Term
The two main data types in Java are _____ and _____.
Definition
primitives and reference types
Term
True or false - a reference to an object holds an actual instance of the object in the variable.
Definition
False, the reference is only a pointer or memory address to where the object actually is in memory.
Term
Explain a reference variable?
Definition
A reference variable points to an object in memory and  is used to access the various fields and methods in the object.
Term
True or false - Primitives have methods.
Definition
False
Term
Show how you can declare (with default initialization) several variables at once.
Definition
String s1, s2, s3;
Term
Show how you can declare multiple variables at the same time, and also execute in-line initialization for each one.
Definition
String s1 = "s1", s2 = "s2", s3 = "s3";
Term

What happens in the following statement?

 

int i1, i2, i3, i4 = 4;

Definition
i1, i2, i3 are all declared, but only one was initialized, i4 to the value of 4
Term

True or false - The following declaration statement is legal;

 

String s1, int num;

Definition
False this does not compile.
Term

Which of the following are valid in Java?

1. boolean b1, b2;

2. String s1 = "1", s2;

3. double d1, double d2;  

4. int i1; int i2; int i3; i4;

Definition

1. Legal

2. Legal

3. Not legal, Java does not allow you to declare more than 1 type in a declaration

4. I1, i2, i3 are legal. I4 is not legal.

 

Term
What are the 3 rules for identifiers for variables, methods, classes, and fields?
Definition

1. A name must begin with a letter, $ or _.

2. Subsequent characters may also be numbers.

3. You cannot use the same name as a Java keyword.

Term
True or false - method or local variables can be used first without being initialized.
Definition
False, local/ method variables do not have a default value and must be initialized before being used in the code.
Term
True or false - Instance and Class variables are initialized to default values.
Definition
True - byte, short, int, long = 0, float, double = 0.0, boolean = false, char = '\u0000' (or NUL), and Objects or reference types are initialized to null
Term
True or false - All Java Objects are stored in the memory's stack.
Definition
False, they are stored in the memory's heap.
Term
What are the two rules for garbage collection eligibility?
Definition

1. The object no longer has an references pointing to it.

2. All references to the object have gone out of scope.

Term
True or false - The built-in finalize method, even if not implemented, can get called multiple times.
Definition
False, if it is implemented, and if it is called once, it will not be called again. It may not be called at all if the Garbage Collector runs and fails to collect the object. If it tries to collect the object on a subsequent attempt, it will not run the finalize method. The way to think of it is if the object does not have a reference, and/or it is out of scope and can be cleaned up, if the garbage collector runs and it is eliligble, it will attempt to run finalize on the object.
Term

True or false - for byte, short, char you can assign numeric non-decimal literals and it will compile.

i.e.

short s = 5;

byte b = 4;

char ch = 3;

Definition
True
Term
True or false - you can place an underscore at the end of a numeric value.
Definition
FALSE
Term
True or false - logical XOR ^ means that if the right and left side contain different values then it returns true, but if both left and right are true OR both left and right are false, then it returns false. In other words the only way for this to be true is if both sides are different.
Definition
TRUE
Term

What happens in the bytecode when this scenario happens and what is the final value in x?

int x = 7;

x = x++;

Definition

So, what happens is, the JVM has an area called the operand stack that holds the values of reference variables during the course of the reference variable values having some operation done to them, namely incrementing, decerementing, adding, subtracting, etc. Here, the JVM will take the initial value of x or 7 and push it in to the Operand stack. It will then pop it out of the stack back the variable. X = 7.

Then it will push the value of x or 7 back to the operand stack. Then, it tells the reference variable x, which currently has a value of 7 to increment it by 1. The reference variable value is now 8.  But, the next instruction pops the top of the stack back into the reference variable x, which overwrites the 8 back to a 7 and hence has returned 7 as the value of x. Keep in mind if the operation were instead x = ++x; the instruction to increment the value of x would have happened first, and the result of that would have been pushed to the stack and then popped or stored back into x at the end of the operation.

So for x =x++; the answer here is 7.

Term

True or false - the following will compile;

 

StringBuilder sb = "hello";

Definition
False - you cannot create a new StringBuilder object with a String literal!
Term
True or false - Both StringBuilder and ArrayList have constructors that take in a capacity value, and also have methods called capacity() that return the capacity value.
Definition
False - they both have constructors that have capacity parameters, but only StringBuilder has a public method capacity().
Term
True or false - An array does not override the .equals() method, and can only evaluate == to see if it is pointing to the same array object in the heap.
Definition
True
Term

True or false - when using static imports, you must use the parameters as well as the name for methods.

i.e.

import static java.util.Collections.sort(ArrayList<String>);

Definition
False, just use member name only!
Term
True or false - Number is a subclass of Integer.
Definition
False, its the opposite, Integer is a subclass of number.
Term
True or false - only private variables, can be hidden in subclasses.
Definition
False, variables with ANY access modifier can be hidden in subclasses.
Term
True or false - Both Interfaces and Abstract Classes inherit from Object.
Definition
False - the underlying objects do inherit from object but interfaces and abstract classes do not.
Term
True or false - if you override a non-private instance method, and you refer to the object, which is a subclass, through a parent class reference variable, the reference version is invoked.
Definition
False - the version that will be invoked will be the subclass object version.
Term

What will happen in the following scenarios? For these examples, a parent class has a private method called runMe(), that is hidden in a subclass.

 

p = parent class

s = subclass

 

1. P p = new P();

p.runMe(); // is called from a main method in P

2. P p = new S();

p.runMe(); // is called from a main method in P

3. P p = new S();

p.runMe(); // is called from a main method in S

4. S s = new S();

s.runMe(); // is called from a main method in S

5. S s = new S();

s.runMe(); // is called from a main method in P

 

Definition

Invoking hidden (private) methods is dependent on where the call is invoked from.  If the Parent reference invokes the call from the Parent class, then it invokes the parent method p.runMe().

If the Parent reference invokes the call from lets say the subclass, this will not compile because this is a private method.

The Child reference can invoke the hidden method in the child object when its run from the parent main or child main.

If the subclass reference invokes

1. Calls private method in P

2. Calls private method in P

3. Generates an exception because p.runMe() is private

4. Calls hidden method in S

5. Calls hidden method in S

Term
True or false - a single .java file with multiple classes in it, will compile into multiple .class files.
Definition
True
Term
True or false - all objects in java are accessed via a reference variable.
Definition
True
Term
True or false - if an exam question has a class and starts on line 1, and there are no import statements and has fields or uses classes as members in standard java libs that are not imported, you should look for an option answer that says code will not compile.
Definition
True
Term

True or false - it is legal to have division of int variable say 3/5 and store the result in an int ;

i.e.

int d = 3/5;

even though the answer is .6, because its less than zero its rounded down to zero as an int.

also even if you assign the result to a double, the answer is still zero. The  expression is evaluated as two ints dividing so the resultant data type on the right is int, when cast to double gives 0.0.

to make the result 0.6, you must make the 3 and the 5 doubles;

double d = 3.0/5.0; // = 0.6

Definition
True
Term

True or false - when using a compound operator, the left hand side variable must have already been initialized before the expression on the right is evaluated, and will not compile;

i.e.

int x = 1;

int y += 4*x+2;

Definition
True
Term
True or false - by casting an object to a subclass reference, you may add new attributes to the object in memory.
Definition
False - casting an object does not modify the object in memory. If the object being referenced is a super class, it probably has less members accessible, so just down casting to a subclass with more members does not give that object access to the subclass's members (unless they are static).
Term
On the exam, watch out for Indentation on If statements, they may try to trick you by having a for loop index value look like its part of the loop block, but since there are no braces, the usage of the index is lined up with the single line of code in the for.
Definition

For(int index = 0;index < array.length; index++)

System.out.println(array[index]);

System.out.println(index); // this one is outside the loop and out of scope.

Term

Watch out, the exam will try to trick you by switching the psvm syntax - instead of

Public static void main (String[] args)

Definition

static void public main (String[] args)

Return type must be next to Method name!!

Term

Watch out the exam will try to trick you with local variable initialization in a method, that does not initialize all the variables;

 

int x, y = 0;

 

Definition
Because its a local variable, this will not compile. Local variables must be initialized before they are used.
Term
True or false - you are allowed to overload the finalize(Object o) method and it will work the same way as the finalize() method.
Definition
False - this overloaded version will do nothing. Garbage collection may only look at the finalize() method and nothing else.
Term

True or false - Autoboxing is the automatic conversion the Java compiler makes between primitive types and their object wrapper classes. Autoboxing is when

1) a primitive value is passed as a parameter to a method that expects an object of the corresponding wrapper class

2) Assigned to a variable of the corresponding wrapper class.

 

Unboxing is the other way.  Unboxing occurs when an object of a wrapper class is

1) passed as a parameter to a method that expects a value of the corresponding primitive type.

2) Assigned to a variable of the corresponding primitive type.

Definition

True

int x = new Integer(2);

Integer y = x;

Term

True or false - the static keyword cannot be used for top-level class declarations.

i.e.

public static class MyClass{ // static not legal here

 

Definition
True
Term

True or false - importing redundant classes is allowed.

 

package a.b;

import a.b.C;

public class C{

 

}

Definition
True
Term

True or false - if you have multiple instances of a class, the finalize method can be called more than once for the class (once per each instance).

 

Definition
True.
Term
True or false - comments, can go above package statements.
Definition
True
Term

True or false - Primitives can only be autoboxed to their respective Wrapper types regarding method conversion.

 

short s = 1;

public void m(int i); // will compile

public void(Short S); // will compile 

public void(Integer I); // will NOT compile

Definition
True
Term

True or false - for most of the primitive wrappers, there is a constructor that takes in a String param.  There is also a type.typeValue() instance method that is non-static and returns the primitive value of the instance. Also type.parseType is a static method that takes in a String. And when you pass in a String, you do not need to add "L" to the string for these methods.

i.e.

String l = "123";

Long long1 = new("123");

1) new Long(l);

2) long1.longValue();

3) Long.parseLong(l);

Definition
True
Term

True or false - as long as the primitive variables are declared before the "=" operator chaining is legal.

i.e.

int a, b, c;

a = b = c = 9;

In fact if only b an c are declared beforehand, then the following is also valid;

 

int a = b = c = 9;

Definition
True
Term

Remember when you pass a primitive or reference into a method and you reassign its value, the original value of the parameter passed in does not change.

But, when you call a method on the reference, and something changes in the object its refering to, then those changes will take affect.

Definition

 

I.e.

public static void processStacks(Stack X1, Stack x2){

X1.push(new Integer(100));// the method called here WILL change the underlying, original object

x2=x1; // this reassignment will NOT affect the original X2

}

Term
True or false - the getClass() method is a public instance method in Object class.  It is polymorphic, its bound to and returns the name of the class of the actual OBJECT to which the reference points.
Definition

True

public class A{

}

public class AA extends A{

}

public class TestClass{

public static void main(String[] args){

A a = new A();

AA aa = new AA();

a=aa;

// this is where things get weird

System.out.println("a="+a.getClass()); // this references to the class of the object

System.out.println("aa="+aa.getClass()); // this also references the class of the object

 

 

 

}

}

Term
True or false - in wrapper classes, the .equals() method takes in the same type as the calling wrapper class and checks to see if they are equal.
Definition
False - the signature of .equals(Object o) takes an object and the first thing it does is check to see if the objects are the same type and if not, it returns false right away.
Term

True or false - when comparing booleans using == referential equality, the rules are as follows,

1) get both sides to be either wrappers or primitives.

2) if both sides are wrappers, do the compare.

3) if ONLY one side is a wrapper, unbox the wrapper, do the compare.

 

Definition
True
Term

True or false - if you have any lower primitive expression on the right hand side, and assign the result to a higher primitive type no matter which one, it will compile.

i.e.

byte b = 1;

shor s = 2;

s = b * b;

Definition
False - expressions using primitives will automatically be promoted to ints. So for the example above, b*b will result in an int, and this cannot be assigned to a short (unless casted).
Term

True or false - chars get automatically promoted to ints, and since 0 = is the same thing as '0' represented as a char, if we do;

int i = 0;

char c = '0';

Boolean b = (i == c);

b will be true.

Definition
False - the character '0' when promoted to its int equivalent is a completely different value, 48. None of the char versions of numeric values match to their int counterparts and can't be assumed that that is the case.
Term

True or false - if you cast a floating point primitive to an int, it will round the value up to the next int if the decimal is .5 or higher.

 

float f = 5.5f;

int i = (int)f;

Boolean b = (i == 6);

b  is true

Definition

False - it will just give you what's on the left side of the decimal, in this case 

int i = (int)f;

would be equal to 5.

Term
True or false - the == comparison operator compiles and runs using primitive type == primitive type, same object type == same object type, and object type == primitive (and if its a wrapper, the wrapper is unboxed to make it a primitive), but NOT object type == different object type.
Definition
True
Term
True or false - if an reference type is Object and it refers to a String object, when the toString() method is called it is the Strings toString method and not the Object's (which prints class@hashcode) due to the instance method Being invoked via polymorphism.
Definition
True
Term
Be careful on the test when they give an example with a parameter passed to a method that shadows an instance variable, make sure if you want to change the value of the instance variable, you refer to it with "this.".
Definition

<class declaration...>

int k = 5;

public Boolean checkIt(int k){ // shadow method param passed in NOT instance var

    Return k-->0?true:false;

}

public void printThem(){

    While(checkIt(k)){ // this is the instance var, and is not affected by shadow above, any changes need to instance var must be done here

        System.out.println(k);

    }

}

Term

True or false - if you Call the String replace method on a literal, and the replace original value is the same as the replacement value it creates a new String and points to the a new String object.

 

i.e.

 

"String".replace('g', 'g') == "String" (is false)

Definition
False - of the replace values are the same, it points to the same literal.
Term

Which of the following are legal instantiation expressions and why?

1) int a = b = c = 100;

2) int a,b,c; a = b = c = 100;

3) int a = 100, b, c;

4) int a = 100 = b = c;

5) int a, b, c = 100;

Definition

1) Illegal - chaining on declaration is illegal.  If the variables b, c had already been declared, this would be ok.

2) Legal - all variables were previously declared.

3) Legal - a set to 100, b and c with default separated by commas.

4) Illegal - b and c not previously declared.

5) Legal - a, b, declared with default, c declared and set to 100.

 

Term
True or false - the rule of thumb for multiple classes in a file is that only once can be public.  If there is a public class it has to have the same name as the file. If there are no public classes, then a non-public class can have the same name as the file, and it is not a requirement that a class that has the same name as the file must be public. Only that if there is a public class, it must have the name of the file.
Definition
True
Term
True or false - final is a valid modifier of the main method.
Definition
True
Term

True or false - when calling valueOf on a Float wrapper, you can pass in either a string float with no F or one with an F

 

Float f = Float.valueOf("12.3");

f = Float.valueOf("12.3f");

Definition
True
Term
If you are given a problem using the args from main, and there are multiple scenarios to choose from and they are asking which will compile, make sure when you test each choice that you try with args.length = 1, because you may find that this will be a problem.
Definition

Will this code fragment print the last arg from the command line, and exit without any exception or stack trace if no args are given?

 

public static void main (String args[]){

    Int i = args.length -1;

    if(i > 0) System.out.println(args[i]);

 

}

 

1) Test with 0 args - args.length = 0 - 1 = -1. -1 > 0 = false so this test will pass if there are zero args.

2) Test with 1 argument - args.length = 1 - 1 = 0. 0 > 0 = false - This test fails because there is only one argument, and hence it is the last argument and it will not be printed.

3) Test with 2 arguments - args.length = 2 -1 = 1. 1 > 0 = true - This test passes because it does get the last argument, and prints it.

 

Overall, this statement does not meet the requirements because if there is only 1 argument it will not get printed, even though it is the last argument.

Term

Assess the following code and discuss why the value if each variable is calculated.

 

1) Int X1= -4;

2) Int x2 = X1--;

3) Int x3 = ++x2;

4) if (x2 > x3){ --x3;}else{ X1++;}

5) return X1 + x2 + x3;

Definition

1) x1 is assigned -4.

2) x2 is first assigned -4, then after x2 has been assigned, the X1 variable has -1 added to it, so x1 becomes -5.

3) x3 is assigned the increment of x2, or -3 (-4+1), and x2 becomes -3.

4) -3 is not greater than -3 so x1 is incrememted to (-5 + 1) = -4

5) -4 + (-3) + (-3) = -10.

Term

True or false - if you have an ArrayList<Double> l, you can add an int to it.

i.e.

 

l.add(111);

Definition
False - you can't box an int to a Double.
Term

True or false - since ArrayList contains method takes in an Object, you can pass in a String even though the ArrayList may be typed to Double.

 

i.e.

ArrayList<Double> l = new ArrayList();

l.add(1.2);

l.contains("string");

 

Definition
True
Supporting users have an ad free experience!