Test 1 Flashcards

1
Q

What types of objects can be used with an enhanced for loop in Java?

A

An enhanced for loop can be used with either an array or an object of a class that implements the java.lang.Iterable interface.

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

How do you import all static members from a class using static imports?

A

To import all static members from a class using static imports, you can use the syntax:

import static packageName.className.*;

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

Is the java.lang.System class final?

A

Yes, the java.lang.System class is final, which means it cannot be extended. It provides access to system-wide resources and operations.

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

What are covariant return types in Java?

A

Covariant return types allow an overriding method to change the return type to a subclass of the return type declared in the overridden method, introduced since Java 1.5.

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

What does the ArrayList.subList() method in Java return?

A

The ArrayList.subList() method returns a view of a portion of the original list between specified indices, inclusive of the fromIndex and exclusive of the toIndex.

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

What happens when multiple static blocks are defined in a Java class?

A

Multiple static blocks, known as static initialization blocks, execute sequentially from top to bottom as the class is loaded.

They help with initializing static variables and performing other actions during class loading.

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

What happens if the overridden method returns a primitive type in Java?

A

Covariant return types do not apply to primitives. If the overridden method returns a primitive type, the overriding method’s return type must also be the same primitive type.

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

Is the java.lang.Number class final?

A

No, the java.lang.Number class is not final. It serves as a base class for numeric wrapper classes like Integer, Long, and Double.

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

will the following compile if not then why

boolean b1 = false; 
boolean b2 = false;
if (b2 != b1 = !b2)
A

This statement will not compile the reason is that = has least precedence of all operators so the order of evaluation goes

b2 != b1&raquo_space;> false != false&raquo_space;> false // firts
False = !b2&raquo_space;> compile time error because we are trying to assign to the value false // then

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

What are the characteristics of Java string literals?

A

String literals begin and end with double quotes (“ “), can contain one or more characters, and may also be empty.

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

How does the String substring(int beginIndex, int endIndex) method of the String class work?

A

The substring(int beginIndex, int endIndex) method returns a new string that is a substring of the original string, starting from beginIndex and ending just before endIndex.

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

How do you import all classes within a package using standard imports?

A

To import all classes within a package using standard imports, you can use the syntax:

import packageName.*;

This allows access to all members of any class within the specified package, based on their access modifiers.

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

How are instance initializer statements/blocks executed in a class

A

Instance initializer statements/blocks are executed in the order they are defined

execution of these is after the static blocks and before the constructor. They are called when an object of the class is created.

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

What information is printed when you use System.out.println(exception)?

A

The name of the exception class (fully qualified name) along with the message encapsulated in the exception.

Example:

exceptions.MyException: Exception from foo

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

describe the following method from the String class

int indexOf(String str, int fromIndex)

A

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

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

How can you make an object eligible for garbage collection in Java?

A

An object can be made eligible for garbage collection by ensuring there are no references pointing to that object. Once no references exist, the object becomes a candidate for automatic memory reclamation.

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

Is it allowed to put underscores in a type cast?

A

No, underscores are not allowed in a type cast.

Example:

double d = (double) 1_000; - This is invalid.

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

What should a lambda expression return to satisfy the Predicate interface in Java?

A

To satisfy the Predicate interface, a lambda expression must return a boolean value. The test method of the Predicate interface expects a boolean result.

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

describe the following method from the String class

int indexOf(int ch)

A

Returns the index within this string of the first occurrence of the specified character.

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

Are changes made to the returned sublist from ArrayList.subList() reflected in the original list, and vice versa?

A

Yes, non-structural changes made to the returned sublist are reflected in the original list, and vice versa.

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

What does the ArrayList.add(E e) method in Java return?

A

The ArrayList.add(E e) method returns true if the collection was changed as a result of the call, indicating that the element was successfully added.

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

What is the purpose of standard imports in Java?

A

Standard imports are used to bring an entire package or a specific class into scope, allowing access to all members of the imported class or package using their simple names.

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

What are the default access modifiers for fields defined in an interface in Java?

A

Fields defined in an interface are always considered as public, static, and final.

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

in the following code on line 2 what is the order of evaluation and would this code compile, if not, why

Object t = new Integer(107);   
int k = (Integer) t.intValue()/9; // line 2
System.out.println(k);
A

evaluation begins with t.intValue() and then the cast, because the (.) takes precedence over a cast

however the code will not compile because intValue() is not a method of Object

the following code would recify it:

Object t = new Integer(107);   
int k = ((Integer) t).intValue()/9; // line 2
System.out.println(k);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the significance of the final keyword for variables in Java?

A

Declaring a variable as final (final variableType variableName = “value”) means that the value of the variable cannot be changed once it has been instantiated with a value.

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

Can you declare a field as private or protected in an interface in Java?

A

No, you cannot declare a field as private or protected in an interface. They are always implicitly public, static, and final

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

Can you directly invoke the garbage collector in Java?

A

No, you cannot directly invoke the garbage collector. However, you can suggest the JVM to perform garbage collection by calling System.gc();.

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

When does implicit narrowing not apply?

A

Implicit narrowing does not apply to float, long, or double types.

For instance, char ch = 30L; will fail to compile even though 30 is within the range of a char, because implicit narrowing does not work for long to char conversion.

29
Q

describe the following method from the String class

int indexOf(int ch, int fromIndex)

A

Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

30
Q

When do Java string literals refer to the same reference from the string pool (5 scenarios), and what is the exception to this rule?

A

String literals in the following scenarios will refer to the same reference from the string pool:

  • String Literals in Same Class and Package: String literals within the same class and package refer to the same instance in the string pool.
  • String Literals in Different Classes in the Same Package: String literals within different classes in the same package reference the same instance in the string pool.
  • String Literals in Different Classes in Different Packages: Even when in different packages, string literals reference the same instance in the string pool.
  • Concatenation of Compile Time Constant Expressions (Literals or Final Variables): When constant expressions are concatenated (literals or final variables), the result is computed at compile time, resulting in a single string literal stored in the string pool.
  • Explicit String Interning: Interning a string explicitly involves checking if an equivalent string exists in the pool; if so, the existing instance is returned instead of creating a new one.

Exception: The only exception to this rule is “Concatenation at Runtime,” where strings are concatenated using variables or non-final expressions. In such cases, new instances are created outside the string pool.

31
Q

What access modifiers can you use for fields defined in an interface?

A

Fields in an interface can only be public, as they are implicitly public, static, and final.

32
Q

Can a class marked as final be extended by other classes?

A

No, a class marked as final cannot be subclassed or extended by any other classes.

33
Q

What happens if implicit narrowing rules are violated?

A

If implicit narrowing rules are violated, a compile-time error occurs.

For instance, attempting to assign an int literal to a byte variable without adherence to the rules would result in a compile-time error.

34
Q

Do lambda expressions create a new scope in Java?

A

No, lambda expressions do not create a new scope. Be cautious when using variable names in lambda expressions that are already in scope, as they will share the same scope as the variables outside the lambda.

35
Q

What is the order of execution when a class is encountered and loaded by the JVM?

A
  1. Static statements/blocks are called in the order they are defined.

Upon object creation:
2. Instance initializer statements/blocks are called in the order they are defined. (Upon objec)
3. The constructor is called.

36
Q

describe the following method from the String class

` int indexOf(String str) `

A

Returns the index within this string of the first occurrence of the specified substring.

37
Q

Are wrapper classes (e.g., Integer, Boolean) in Java extendable?

A

No, wrapper classes are also final and cannot be extended through inheritance.

38
Q

What does the String substring(int beginIndex) method of the String class do?

A

The substring(int beginIndex) method returns a new string that is a substring of the original string, starting from the beginIndex position.

39
Q

describe the following

Static {}

A

is a static block, Astatic blockis a block of code that is executed only once when the class is loaded.It can access and initialize static variables and methods, but not instance variables or methods

40
Q

Are the classes String, StringBuilder, and StringBuffer in Java extendable?

A

No, all three classes (String, StringBuilder, and StringBuffer) are final classes, meaning they cannot be extended through inheritance.

41
Q

When using exception.printStackTrace(), what information is included in the output?

A

The output includes the name and message of the exception at the top, followed by a complete chain of method names and their corresponding line numbers. This chain represents the sequence of method calls leading to the creation of the exception, going back to the point where the thread was started.

Example:

exceptions.MyException: Exception from foo
        at exceptions.TestClass.foo(TestClass.java:22)
        at exceptions.TestClass.hello(TestClass.java:18)
        at exceptions.TestClass.main(TestClass.java:5)

Note that:
The method calls start from where the exception was first created and thrown and follows all method calls that led to that

42
Q

can numbers with undscores be given to a method or as an array size

A

NO,

Underscores are not allowed in a value passed to a method or in an array size. Example: System.out.println(1_234); and int[] arr = new int[10_000]; - Both of these are invalid.

43
Q

what will the indexOf() method of the String class return if it does not find a match

A

indexOf returns -1 if no such character or string exists

44
Q

What is the general rule for using underscores for numeric literals?

A

The rule for using underscores in numeric literals is to place the underscore between two digits.

Underscores cannot appear at the beginning or end of a number, adjacent to a decimal point, or prior to an F or L suffix.

45
Q

Are underscores allowed as the first character in a variable name?

A

Yes, an underscore is allowed as the first character in a variable name.

46
Q

What is the implication of fields in an interface being public, static, and final?

A

The implication is that you cannot assign values to interface fields outside the interface definition.

47
Q

How is a specific class imported using standard imports?

A

To import a specific class using standard imports, you can use the syntax:

import packageName.className;

This gives access to all members of the class based on their access modifiers.

48
Q

What is the purpose of implicit narrowing in Java?

A

Implicit narrowing provides a convenience feature that allows direct assignment of constant values without explicit casting for types byte, short, and char, as long as the value falls within the range of the target variable’s type.

49
Q

Does the Map interface in Java implement the Iterable interface?

A

No, the Map interface does not implement the Iterable interface directly.

50
Q

Can you redeclare a variable within the same scope in Java?

A

No, in Java, it is not possible to redeclare a variable within the same scope. Once a variable is declared (Has type and name), its declaration remains constant and cannot be changed in that scope.

51
Q

How does the ArrayList.subList() method handle the case when fromIndex and toIndex are equal?

A

If fromIndex and toIndex are equal, the returned list is empty.

52
Q

Why are all methods in the String class considered implicitly final?

A

The String class itself is final, which means it cannot be subclassed. As a result, all of its methods are implicitly final and cannot be overridden.

53
Q

What is the purpose of the longValue() instance method in the Long class?

A

The longValue() method returns the primitive long value held by the Long object it is called on.

54
Q

What is the purpose of static imports in Java?

A

Static imports are used to import static members (fields and methods) from a class, allowing you to use them directly without using the class name.

55
Q

How are Long objects created using static methods?

A
  • Long valueOf(String s) - Creates a Long object from a string representation of a number.
  • Long valueOf(long l) - Creates a Long object from a primitive long value.
56
Q

Why are imports used in Java?

A

Imports allow you to use the imported elements by their simple names, reducing the need to use their fully qualified names, which can make code cleaner and more readable.

57
Q

describe the following (when found in the class body)

{}

A

is an instance block, Aninstance blockis a block of code that is executed every time an object of the class is created.It can access and initialize both static and instance variables and methods

An instance block is executed before the constructor, but after the static block

58
Q

What is implicit narrowing in Java?

A

Implicit narrowing is a special case of assignment conversion that allows a constant expression of type byte, char, short, or int to be assigned to a variable of type byte, short, or char without an explicit cast, as long as the value of the expression is within the range of the variable type.

59
Q

What is the purpose of the Long.parseLong(String s) static method?

A

The parseLong(String s) method is used to convert a string representation of a number into a primitive long value.

60
Q

Give an example of implicit narrowing.

A

Example:

byte b = 127;

Here, the literal 127 of type int can be implicitly narrowed to fit within the range of type byte.

61
Q

What constructors does the Long wrapper class have?

A

The Long wrapper class has two constructors:

Long(long value) - Creates a Long object from a long value.
Long(String s) - Creates a Long object from a string representation of a number.

NOTE THAT LONG (and other wrapper classes) does not have a no-args constructor. Therefore, new Long() without arguments would be invalid.

62
Q

What does using the final keyword before a method prevent in Java?

A

Applying the final keyword to a method prevents that method from being overridden by subclasses, ensuring its implementation remains unchanged.

63
Q

What are the requirements for implicit narrowing to occur?

A

Implicit narrowing occurs when:

  • The expression is a compile-time constant expression of type byte, char, short, or int.
  • The type of the variable is byte, short, or char.
  • The value of the expression is representable in the type of the variable.
64
Q

Can multiple continuous underscores appear between two digits?

A

Yes, multiple continuous underscores can appear between two digits. For example, 2____3 is equivalent to 2_3 and the same as 23.

65
Q

Does the behavior of returning true apply when adding an element at a specified index using ArrayList.add(int index, E element)?

A

No, the behavior of returning true does not apply when using ArrayList.add(int index, E element) to add an element at a specified index. The return type of this method is void, and it does not indicate if the collection changed.

66
Q

Does the ArrayList.subList() method create a new copy of the sublist’s elements?

A

No, the returned sublist is backed by the original list, meaning they share the same underlying data.

67
Q

How do you import a specific static member from a class using static imports?

A

To import a specific static member from a class using static imports, you can use the syntax:

import static packageName.className.staticMemberName;

68
Q

Can the main method be in an abstract class

A

Yes, you can have amain() methodin anabstract classin Java. The main() method is astatic methodso it is associated with the class, not the object or instance.The abstract keyword is applicable to the object so there is no problem if the class contains the main() method1.