OCA Java SE 8 Programmer I Flashcards

1
Q

If package component is not defined in a class, where does this class resides?

A

In a default package

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can a class have 2 packages defined?

A

No, it will fail to compile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can import statement be placed before package statement?

A

No, it will fail to compile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you structure multi-line comments?

A

/*
* This
* is
* a comment
*/

Asterisks in every line are not mandatory but they can often be found due to aesthetics reasons.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the name of java compiler in Windows and Unix systems?

A

Windows - javac.exe
Unix - javac

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between multi-line comments and Javadoc comments?

A

Java doc comments start with /**
They are processed with Javadoc which is a JDK tool for generating API documentation of Java classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are non-access modifiers?

A
  • final
  • static
  • abstract
  • synchronized
  • native
  • transient
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can you use Class instead of class to define a class? :)

A

No, it is case sensitive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a class?

A

Class is a blueprint out of which object is created

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can you define multiple classes and/or interfaces in a single Java source file?

A

Yes, how much you want and in any order you want

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How many public classes/interfaces can you define in a single Java source code file?

A

You can define only one public class/interface and name that follows has to match the name of a Java source code file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

If you have defined multiple classes with a Java source code file is the import statement propagated to all defined classes?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can you define more than one executable class?

A

Yes, you only need to decide which one to call on start-up

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Define a main metod

A

public static void main(String args[]) {…}
public static void main(String… args) {…}
static public void main(String args[]) {…}
public static void main(String[] args) {…}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can you define a main method with different signature?

A

You can, but if you want to run the program you need also a main method with the right signature. Having 2 main methods in the same class like:
- public static void main(String args[]) {…} //will be executed
- public static void main(double number) {…}

is okay since you are only overloading the main method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write down an example of running a Java class with command line parameters

A

java MyClass 1 2 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Are package names case sensitive?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to run a Java class from terminal if you are in different folder than that specific class?

A

You have to add the base directory to the classpath. Most convenient way would be to do it like:
java -classpath /Work/Projects/java8-certification test.MyClass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

If you use an import java.lang.* does the size of your class increases?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

If two packages are located in the same level, how do you import a class for another package?

A

You need to import it using the entire filepath (all packages from base directory)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

When you use a class defined in java.lang package do you need to import java.lang package?

A

No, all classes automatically have java.lang package imported

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Is it possible to use two classes with the same name that are located in different packages (ex. java.util.Data and java.sql.Data) in a particular class?

A

It is possible, by defining variables like:
java.util.Date date1;
java.sql.Data date2;

Using import statements for both of them is not possible!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

When you use an import statement like import net.netconomy.* what classes are actually accessable?

A

Only classes defined directly in net.netconomy folder, classes that are defined in the sub-folder are not imported.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How to access classes defined in default package?

A

Default package classes can only be accessible from the classes that are also in a default package and located in the same directory - classes with named packages are not able to access them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What are static imports?

A

Static imports are direct imports of public static attributes and methods for another class.
package net.netconomy.animal;

class Cat {
public static int lives = 9;
public void sound() {
System.out.println(“meow!”);
}
}
_________
import static net.netconomy.animal.Cat.*;

class Tiger {
public void roar() {
sound();
}
public int livesCounter() {
return lives;
}
}

Although they are called static imports, you can only use them as “import static”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is a top-level class?

A

A top-level class is a class that is not defined within any other class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

On which type of Java entities access modifiers can be used?

A

They can be used on a class/interface and it’s members (variables and methods). They can’t be used on local variables and method parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What access modifiers can we use on a class?

A

Only public and default (no modifier set). If public is set, class can be accessed outside of the package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What are unrelated classes?

A

Unrelated classes are classes that don’t have inheritance relation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Can related class access protected attributes of inherited classes?

A

Yes, but it can’t access them as reference variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the purpose of non access modifiers?

A

They are changing the default behavior of a class, interface or method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Can you use abstract modifier on interface?

A

You can, but it is redundant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Can you use final on a class?

A

You can use it and that means that the corresponding class cannot be extended.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Can you use final on an interface?

A

No, interfaces are abstract by default, code won’t compile.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Will this code compile?
final StringBuilder name = new StringBuilder(“dobar”);
name.append(“dan”);

A

It will compile, you can’t change the value of a final variable directly but you can call a method that will change it’s state in the background.

36
Q

Can you use final on a method?

A

Yes, and that method can’t be overriden by inherited class.

37
Q

Can static method access non-static variable?

A

No, because non-static variable is created when instance is created and static method lives in the class.

38
Q

If a class has static variables and the class is initialized to “null”, can you access static variable values?

A

Yes, they are not connected with class initialization.

39
Q

Can you prefix a class or an interface with “static”?

A

You can but not for top-level classes, only nested ones.

40
Q

What is the main difference between Java and languages like C or C++?

A

For C and C++ code has to be recompiled for each system like Windows, Linux, etc. while for Java you can run it via JVM on any system without recompiling.

41
Q

What are the main features and components of Java?

A

Type safety
Object orientation
Encapsulation
Polymorphism
Abstraction
Automatic memory management

42
Q

Can a non access modifer “abstract” be used on a class if it doesn’t have any abstract methods defined?

A

Yes

43
Q

What are 8 main primitive types?

A

char
short
long
int
double
float
boolean
byte

44
Q

Define a categorization of primitive types

A

numerical Boolean
signed. | unsigned boolean
integers | floating point char

byte | int | short | long double | float

45
Q

What is unsigned numeric type?

A

It can only store positive integer values like “char”

46
Q

Can you define a variable and skip assigning a specific type to it like “price = 12.9;”

A

No, Java is a typed language

47
Q

What is a literal?

A

That is a value for a specific type that can be used, like true/false for boolean primitive type.

48
Q

What is the size in bites for each signed primitive type?

A

byte | 8 bits | -128 to 128
short | 16 bits | -32768 to 32767
int | 32 bits | ~ 2.000.000.000 (+/-)
long | 64 bits |

49
Q

What are basic integer literal values?

A

binary
octal
decimal
hexadecimal

50
Q

How are binary, octal, decimal and hexadecimal values assigned?

A

binary -> 0b10001
octal -> 0413
decimal -> normal number
hexadecimal -> 0x10B

51
Q

Is this a valid code?
long number = 100_267_760

A

Yes

52
Q

Can you use “_” in numbers?

A

Yes, you can use _ between digits but not on the end or begining. It was designed for grouping digits for different number sytems like decimal, hexadecimal, etc

53
Q

Explain the advantages and disadvantages of float vs double

A

float is smaller (32 bits vs 64 bits)
double is more precise

54
Q

Provide few examples of assigning value to char

A

char kar = ‘A’;
char kar = ‘123’;
char kar = ‘\u0122’;

55
Q

How can you assign negative integer value to char?

A

You can do it by casting -> char kar = (char) -242;
The thing is that during conversion “sign” bit is also stored as part of the value which results in strange characters like ナ
Is see it as distortion of a value

56
Q

Are hyphens allowed in identifiers?

A

No, only underscore

57
Q

What are valid indentifier names?

A
  • Starts with a-z (big or small)
  • Can contain digit but not on the first place
  • Can contain _ on any space
  • Can contain currency sign on any space
58
Q

When you can’t use an underscore in a number?

A
  • before or after .
  • before letters like F or D in float or double
59
Q

What are the 2 types of variables?

A

primitive and refernce(object) variables

60
Q

What is a literal value for all object refernces?

A

null

61
Q

How to assign previously defined integer variables a and b to value 10?

A

a = b = 10;

62
Q

Can you store larger range of values like long into int?

A

You can, but only with casting

63
Q

Can you assign multiple variables in the same row?

A

int a = 5, b = 6, c = 10;

64
Q

What is unary decrement operator?

A

–a or a++

65
Q

What is the primitive type of a result when adding two primitive values?

A

Adding two primitive types that are int or smaller from int result always int (ex. byte + short = int) or they result in long if long is one of the numbers

66
Q

How can you make this code compile?
short shorty = 123;
byte bajti = 100;
short rez = shorty + bajti;

A

final short shorty = 123;
final byte bajti = 100;
short rez = shorty + bajti;

67
Q

What will this code return?
public static void print() {
int a = 5;
int b = 7;
int c = 10;
if (a < 3 || b < 10 && c > 20 || b < 9) {
System.out.println(“YES”);
} else {
System.out.println(“NO”);
}
}

A

YES

68
Q

What is autoboxing?

A

Assigning primitive value to a wrapper class, ex. Double d = 29.3;

69
Q

Will this code compile? Boolean.parseBoolean(“TrUe”);

A

Yes, String argument isn’t case sensitive.

70
Q

What is the difference when creating wrapper instance using constructor and valueOf() method?

A

Wrapper classes like Character, Byte, Short, Integer, Long have cached values of -128 to 128. Every time valueOf() method is used with this range of values, cashed values are returned.

71
Q

What will be result of this code?
Integer a = Integer.valueOf(10);
Integer b = 10;
return a == b;

A

true, autoboxing is also taking cached value

72
Q

Can you compare wrapper classes of different types like Short and Long with ==?

A

Code won’t compile

73
Q

What are different scopes of variables?

A
  • Local scope
  • Method parameters scope
  • Instance scope
  • Class scope
74
Q

How can you call a garbage collection?

A

You can’t really call it, there are methods like System.gc() but there isn’t a guarantee that garbage collection will be initiatied.

75
Q

What is a difference between method parameters and arguments?

A

Arguments ar the actual values transferred to a method.

76
Q

Can you define multiple variable arguments as method params?

A

No, only one and it has to be last one in the list.

77
Q

Is this code valid?
void test(int a,b,c) {}

A

No

78
Q

What is an initializer block?

A

It is a block of code { } similar to constructors which is called on creation of an object but before constructors. There are usually used within anonymous classes (type of inner classes without name) since constructors can’t exist.

79
Q

Can you call a default constructor (without arguments) if you have a user defined constructor?

A

No, if you have a user-defined constructor default one is not created.

79
Q

Can you call a default constructor (without arguments) if you have a user defined constructor?

A

No, if you have a user-defined constructor default one is not created.

80
Q

How to call a constructor from another constructor within the same class?

A

this(“name”, 0)
It can be called only once and it has to be on the first line.

81
Q

What is the difference when initializing a string value with new String(“some value”) and with equals sign like = “some value”?

A

Both of them create a new object in the memory, but if you compare two strings that have been initialized with the same literal value like:
String s1 = “some value”;
String s2 = “some value”;

comparing them like s1 == s2 will return true since they will refer to the same object in the memory. Initializing a String with this value is stored in a pool of Strings.

82
Q

What is the main difference between StringBuilder and StringBuffer?

A

Both of them represent mutable implementation of String (immutable) but StringBuffer is providing synchronized support for multiple threads. Always use StringBuilder when you have only one thread running since it is faster.

83
Q

How String ensures immutability?

A

When a new String is initialized, literal value is stored in a private final array of characters. Immutability is ensured since it is an array (size cannot change after initialization) and it has a final non-access modifier

84
Q

If String is an immutable object how come methods like replace() are replacing its value?

A

They are not replacing value of that String, they return a completely new String.

85
Q

What method does String have?

A

Query positions of chars:
- indexOf()
- chartAt()
Seem to modify String:
- substring()
- trim()
- replace()
Other:
- length()
- startsWith()
- endsWith()