General Flashcards

1
Q

All Java code is written inside…

A

Classes

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

Where can code blocks be used?

A

Anywhere a single statement is expected

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

What are the 3 different types of Java comments?

A
  1. Single-line comments //
  2. Multi-line comments /* */
  3. Documentation comments /** */
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the 8 Java primitive types?

A
  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. char
  8. boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. How many bits are in an int and long?
  2. How many bits are in a float and double?
  3. How many bits are in a char?
A
  1. 32 bits (4 bytes) and 64 bits (8 bytes)
  2. 32 bits (4 bytes) and 64 bits (8 bytes)
  3. 16 bits (2 bytes)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. What is the default integer type?
  2. What is the default floating-point type?
A
  1. int
  2. double
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give an example of a long literal

A

100L

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

True or False

true == 1
false == 0
A

False. There are no “truthy” or “falsy” values in Java

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

True or False

Does a code block define a scope?

A

Yes. variables declared inside a code block will no longer be reachable once the block is exited

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

True or False:

Inner scopes have access to enclosing outer scopes, but outer scopes do not have access to inner scopes.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. What are the 6 relatinoal operators?
  2. What type do they evaluate to?
A
  1. > , >=, <, <=, ==, !=
  2. booleans
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Is it possible to have multiple methods with the same name inside the same class?

A

Yes, but only if the methods have different parameter types, number of parameters, or order of parameters. Return types are not considered for method overloading

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

What are the 5 ways to use “final”?

A
  1. Classes
  2. Properties
  3. Methods
  4. Method parameters
  5. Local variables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

True or False

Does an inner class definition have access to the private members of its enclosing classes?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. If a method defines normal and variable parameters, where on the parameter list should varargs be used?
  2. Can a method use multiple varargs?
A
  1. Varargs should be used at the end of the method parameter list
  2. No. A method cannot define multiple vargargs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If a subclass object is assigned to a superclass type, and the subclass object overrides the methods of the superclass, which methods will be called?

A

If a subclass object is assigned to a superclass type, and the subclass object overrides the methods of the superclass, the methods of the subclass object will be called

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

What are the 2 ways to use super?

A
  1. To access the members of an enclosing class
  2. To call a superclass constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

When a subclass is instantiated, are all superclass constructors called?

A

Yes

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

Which class is the ancestor of all objects?

A

Object

20
Q

True or False

A class can extend multiple classes and implement multiple interfaces

A

False. While a class can implement multiple interfaces, it cannot extend multiple classes

21
Q

Can an interface define private methods?

A

Yes. However, private methods can only be used by other private or default methods

22
Q

What is reflection?

A

Reflection is the ability to interface with class definitions at runtime

Object obj = new Object();
Method[] methods = obj.getClass().getMethods();
23
Q

What is the difference between Object.equals(...) and Comparable.compareTo(...)?

A

Object.equals(...) tests for equality by returning a boolean while Comparable.compareTo(...) compares for order by returning an integer representing less than, equals, or greater than (-1, 0, 1 respectively)

24
Q

What is the behavior of ==?

obj1 == obj2
A

== is checking to see if obj1 and obj2 refer to the same object in memory

25
Q

Are lowercase characters greater than upper case characters?

A

Yes. ‘a’ is greater than ‘A’

26
Q

What will get printed for the following code?

int i = 10;
System.out.println(++i);
System.out.println(i++);
System.out.println(i);
A
  1. 11
  2. 11
  3. 12
27
Q

Can an instance change the value of a public static field of its class if it is not declared as final?

A

Yes

28
Q

List the access modifiers from most accessible to least accessible

A
  1. public
  2. protected
  3. package-private
  4. private
29
Q

What is accessible to B from A?

class A {
    private char a = 'a';
    public char getA() {return a;}
}
class B extends A {}
A

getA()directly and a indirectly through getA()

30
Q

What are the differences between overloading and overriding?

A
  1. While return types are not considered for overloading, they are considered for overriding. The return type of the overriding method must be the same or a subclass of the return type of the method being overriden
  2. By definition, overloading requires different method parameters while overriding requires the same method parameters
31
Q

What are the 2 ways to use “this”?

A
  1. To refer to the invoking object
  2. To invoke a member constructor inside another member constructor
32
Q

True or false:

Overriding and hiding are the same thing

A

False. Overriding is a common pattern in Java is used to implement dynamic method dispatch. Hiding is an anti-pattern and only pertains to fields

33
Q

When is casting necessary?

A

Casting is necessary when a superclass object is referenced to a subclass type

34
Q

True or false:

If 2 objects are equal, they always have the same hashcode. And if 2 objects have the same hashcode, they are also always equal

A

False

For example:

obj value = 12345, hashcode = 15 (sum of values)

obj2  value = 54321, hashcode = 15 (sum of values)
35
Q

When should you use checked exceptions and when you should you use unchecked exceptions?

A

You should only use checked exceptions when you want to force the caller to treat that exception. Otherwise, use runtime exceptions and document them

36
Q

What are the 3 different types of retention policies?

A
@Retention(RetentionPolicy.SOURCE) // discarded by the compiler
@Retention(RetentionPolicy.CLASS) // retained in .class file by compiler
@Retention(RetentionPolicy.RUNTIME) retained in .class file and at run time, can be read reflectively
37
Q

What is a JAR file?

A

A JAR file is a package of class files that can run as an executable

38
Q

Can an interface declare non-static fields?

A

No. An interface can only declare public, static, final fields. However, an abstract class can declare non-static fields

39
Q

Which class is the superclass of all errors and exceptions?

A

Throwable

40
Q

What is a checked exception?

A

A checked exception is any exception other than Error, RuntimeException, or any of their subclasses. Checked exceptions are required to be declared or handled by the method in which they are thrown

41
Q

True or False

Does a static nested class have access to the private members of its enclosing class(es)?

A

No

42
Q

True or False

Does a subclass have access to the private members of a superclass?

A

No

43
Q

True or False

When overriding a method that has a throws clause the overriding method must also declare a throws clause

A

False. While the overriding method can also declare a throws clause with the same or subclass exception, it is not required

Valid code:

class A {
    void example() throws Exception {}
}

class B extends A {
    @Override
    void example() {}
}
44
Q

What is the default implementation of object.equals(o)

A

object == o

45
Q

Code an example using varargs

A
function f(...args) {
  console.log(Array.isArray(args));
}

f(1,2,3);

Prints true

46
Q

What is a classpath? What entity uses the classpath?

A

A classpath is a path to class files. The JVM uses the classpath to load and execute Java programs