Java Skill Assessment Quiz Guide
Java Skill Assessment Quiz Guide
main
Java
Q1. Given the string "strawberries" saved in a variable called fruit, what would
[Link](2, 5) return?
rawb
raw
awb
traw
Reasoning: The substring method is accepting two arguments.
The first argument being the index to start(includes that char at 2)
and the second the index of the string to end the substring(excludes the char
at 5).
Strings in Java are like arrays of chars.
Therefore, the method will return "raw" as those are the chars in indexes 2,3,
and 4.
You can also take the ending index and subtract the beginning index from it,
to determine how many chars will be included in the substring (5‑2=3).
Q2. How can you achieve runtime polymorphism in Java?
[Link] 1/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
method overloading
method overrunning
method overriding
method calling
Q3. Given the following definitions, which of these expression will NOT evaluate to
true?
boolean b1 = true, b2 = false; int i1 = 1, i2 = 2;
(i1 | i2) == 3
i2 && b1
b1 || !b2
1: class Main {
2: public static void main (String[] args) {
3: int array[] = {1, 2, 3, 4};
4: for (int i = 0; i < [Link](); i++) {
5: [Link](array[i]);
6: }
7: }
8: }
interface Interface1 {
static void print() {
[Link]("Hello");
}
}
interface Interface2 {
static void print() {
[Link]("World!");
[Link] 2/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
}
}
[Link](); [Link]();
[Link]();
[Link]();
[Link](); [Link]();
CD
CDE
D
"abcde"
Q7. What is the result of this code?
class Main {
public static void main (String[] args){
[Link](print(1));
}
static Exception print(int i){
if (i>0) {
return new Exception();
} else {
throw new RuntimeException();
}
}
}
interface One {
default void method() {
[Link]("One");
}
}
interface Two {
default void method () {
[Link]("One");
}
}
class Main {
public static void main (String[] args) {
List list = new ArrayList();
[Link]("hello");
[Link](2);
[Link]([Link](0) instanceof Object);
[Link]([Link](1) instanceof Integer);
}
}
package mypackage;
public class Math {
public static int abs(int num){
return num < 0 ? -num : num;
}
}
package [Link];
public class Math {
public static int abs (int num) {
return -num;
}
}
import [Link];
import [Link].*;
class Main {
public static void main (String args[]){
[Link]([Link](123));
}
}
Explanation: The answer is "123". The abs() method evaluates to the one inside
[Link] class, because The import statements of the form:
import [Link].*
1: class MainClass {
2: final String message(){
3: return "Hello!";
4: }
5: }
class Main {
public static void main(String[] args) {
[Link](args[2]);
}
}
[Link] 6/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
class Main {
public static void main(String[] args){
int a = 123451234512345;
[Link](a);
}
}
"123451234512345"
Nothing ‑ this will not compile.
a negative integer value
"12345100000"
Reasoning: The int type in Java can be used to represent any whole number from
‑2147483648 to 2147483647. Therefore, this code will not compile as the
number assigned to 'a' is larger than the int type can hold.
Q14. What is the output of this code?
class Main {
public static void main (String[] args) {
String message = "Hello world!";
String newMessage = [Link](6, 12)
+ [Link](12, 6);
[Link](newMessage);
}
}
[Link] 7/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
for ([Link]()) {}
[Link]("apple".compareTo("banana"));
positive number
negative number
compilation error
Q17. You have an ArrayList of names that you want to sort alphabetically. Which
approach would NOT work?
[Link]([Link](String::toString))
[Link](names)
[Link]([Link])
new Date([Link]())
[Link]()
[Link]().getTime()
[Link] 8/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
Q20. Fill in the blank to create a piece of code that will tell whether int0 is
divisible by 5 :
boolean isDivisibleBy5 = _____
int0 % 5 == 0
int0 % 5 != 5
[Link](int0, 5)
Q21. How many times will this code print "Hello World!"?
class Main {
public static void main(String[] args){
for (int i=0; i<10; i=i++){
i+=1;
[Link]("Hello World!");
}
}
}
10 times
9 times
5 times
infinite number of times
Explanation: Observe the loop increment. It's not an increment, it's an
assignment(post).
Q22. The runtime system starts your program by calling which function first?
print
iterative
hello
main
Q23. What code would you use in Constructor A to call Constructor B?
/* Constructor B */
[Link] 9/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
import [Link];
[5, 1, 10]
[10, 5, 1]
[1, 5, 10]
[10, 1, 5]
Q26. What is the output of this code?
[Link] 10/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
class Main {
public static void main(String[] args){
String message = "Hello";
for (int i = 0; i<[Link](); i++){
[Link]([Link](i+1));
}
}
}
"Hello"
A runtime exception is thrown.
The code does not compile.
"ello"
Q27. Object‑oriented programming is a style of programming where you organize
your program around _ rather than _ and data rather than logic.
functions; actions
objects; actions
actions; functions
actions; objects
Q28. What statement returns true if "nifty" is of type String?
"nifty".getType().equals("String")
"nifty".getType() == String
"nifty".getClass().getSimpleName() == "String"
import [Link].*;
class Main {
public static void main(String[] args) {
List<Boolean> list = new ArrayList<>();
[Link](true);
[Link]([Link]("FalSe"));
[Link]([Link]);
[Link]([Link]());
[Link]([Link](1) instanceof Boolean);
}
}
[Link] 11/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
1: class Main {
2: Object message(){
3: return "Hello!";
4: }
5: public static void main(String[] args) {
6: [Link](new Main().message());
7: [Link](new Main2().message());
8: }
9: }
10: class Main2 extends Main {
11: String message(){
12: return "World!";
13: }
14: }
[Link] 12/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
A, B, and D
A, C, and D
C and D
A and D
Explanation: Error is not inherited from Exception
class Main {
static int count = 0;
public static void main(String[] args) {
if (count < 3) {
count++;
main(null);
} else {
return;
}
[Link]("Hello World!");
}
}
[Link] 13/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
import [Link].*;
class Main {
public static void main(String[] args) {
String[] array = {"abc", "2", "10", "0"};
List<String> list = [Link](array);
[Link](list);
[Link]([Link](array));
}
}
[abc, 0, 2, 10]
class Main {
public static void main(String[] args) {
String message = "Hello";
print(message);
message += "World!";
print(message);
}
static void print(String message){
[Link](message);
message += " ";
}
}
Hello World!
HelloHelloWorld!
Hello Hello World!
Hello HelloWorld!
Q37. What is displayed when this code is compiled and executed?
[Link] 14/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
x
null
10
5
Q38. Which approach cannot be used to iterate over a List named theList?
A
Iterator it = [Link]();
for ([Link]()) {
[Link]([Link]());
}
[Link]([Link]::println);
an alphanumeric character
a negative number
a positive number
a ClassCastException
Q44. You get a NullPointerException. What is the most likely cause?
[Link] 16/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
[Link](new Duck("Waddles"));
[Link](new Waddles());
executed; interpreting
executed; compiling
compiled; executing
compiled, translating
Q48. Given this class, how would you make the code compile?
[Link] 17/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
public TheClass() {
x += 77;
}
public TheClass() {
x = null;
}
public TheClass() {
x = 77;
}
Explanation: final class members are allowed to be assigned only in three places:
declaration, constructor or an instance‑initializer block.
Q49. How many times f will be printed?
[Link] 18/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
}
}
4
3
5
A Runtime exception will be thrown
Q50. Which statements about abstract classes are true?
1, 2, and 3
only 3
2 and 3
only 2
Q51. Which keyword lets you call the constructor of a parent class?
parent
super
this
new
Q52. What is the result of this code?
1: int a = 1;
2: int b = 0;
3: int c = a/b;
4: [Link](c);
[Link] 20/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
The default Java type which Java will be using for a float variable will
be double.
So, even if you declare any variable as float, what the compiler has to
actually do is to assign a double value to a float variable,
which is not possible. So, to tell the compiler to treat this value as a
float, that 'F' is used.
[Link]("expelliarmus");
new [Link]();
10 10
[Link] 21/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
5 10
10 5
55
Q60. What is the result of this code?
try {
[Link]("Hello World");
} catch (Exception e) {
[Link]("e");
} catch (ArithmeticException e) {
[Link]("e");
} finally {
[Link]("!");
}
Hello World
It will not compile because the second catch statement is unreachable
Hello World!
It will throw runtime exception
Q61. Which is not a java keyword
finally
native
interface
unsigned
Explanation: native is a part of JNI interface
Q62. Which operator would you use to find the remainder after division?
%
//
DIV
Reference
Q63. Which choice is a disadvantage of inheritance?
Overridden methods of the parent class cannot be reused.
[Link] 22/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
Responsibilities are not evenly distributed between parent and child classes.
Classes related by inheritance are tightly coupled to each other.
The internal state of the parent class is accessible to its children.
Reference
Q64. Declare and initialize an array of 10 ints.
Array<Integer> numbers = new Array<Integer>(10);
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
[Link]("Press me one more time..");
}
});
Reference
Q66. Which functional interfaces does Java provide to serve as data types for
lambda expressions?
Observer, Observable
Collector, Builder
Filter, Map, Reduce
Consumer, Predicate, Supplier
[Link] 23/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
Reference
Q67. What is a valid use of the hashCode() method?
encrypting user passwords
deciding if two instances of a class are equal
enabling HashMap to find matches faster
moving objects from a List to a HashMap
Reference
Q68. What kind of relationship does "extends" denote?
uses‑a
is‑a
has‑a
was‑a
Reference
Q69. How do you force an object to be garbage collected?
Set object to null and call [Link]()
Set object to null and call [Link]()
Set object to null and call [Link]().runFinalization()
There is no way to force an object to be garbage collected
Reference
Q70. Java programmers commonly use design patterns. Some examples are the _,
which helps create instances of a class, the _, which ensures that only one instance
of a class can be created; and the _, which allows for a group of algorithms to be
interchangeable.
static factory method; singleton; strategy pattern
strategy pattern; static factory method; singleton
creation pattern; singleton; prototype pattern
singleton; strategy pattern; static factory method
Q71. Using Java's Reflection API, you can use _ to get the name of a class and _ to
retrieve an array of its methods.
[Link]().getSimpleName(); [Link]().getDeclaredMethods()
[Link] 24/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
[Link](); [Link]()
Refl[Link](this); Refl[Link](this)
Refl[Link](this).getName(); Refl[Link](this).getMethods()
Q72. Which is not a valid lambda expression?
a -> false;
Q73. Which access modifier makes variables and methods visible only in the class
where they are declared?
public
protected
nonmodifier
private
Q74. What type of variable can be assigned to only once?
private
non‑static
final
static
Q75. How would you convert a String to an Int?
"21".intValue()
[Link]("21")
[Link]("21")
[Link]("21")
Q76. What method should be added to the Duck class to print the name Moby?
Duck(String name) {
[Link] = name;
}
[Link] 25/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
&
Reference
Q78. How many times does this loop print "exterminate"?
two
four
three
five
Q79. What is the value of myCharacter after line 3 is run?
p
r
e
[Link] 26/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
i
Q80. When should you use a static method?
when your method is related to the object's characteristics
when you want your method to be available independently of class instances
when your method uses an object's instance variable
when your method is dependent on the specific instance that calls it
Q81. What phrase indicates that a function receives a copy of each argument
passed to it rather than a reference to the objects themselves?
pass by reference
pass by occurrence
pass by value
API call
Q82. In Java, what is the scope of a method's argument or parameter?
inside the method
both inside and outside the method
neither inside nor outside the method
outside the method
Q83. What is the output of this code?
5
8
1
3
Q84. Which change will make this code compile successfully?
[Link] 27/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
import [Link].*;
class Main {
public static void main(String[] args) {
String[] array = new String[]{"A", "B", "C"};
List<String> list1 = [Link](array);
List<String> list2 = new ArrayList<>([Link](array));
List<String> list3 = new ArrayList<>([Link]("A", new String("B"),
[Link]([Link](list2));
[Link]([Link](list3));
}
}
falsefalse
truetrue
falsetrue
truefalse
Q86. Which code snippet is valid?
ArrayList<String> words = new ArrayList<String>(){"Hello", "World"};
[Link] 28/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("hello");
[Link](0).insert(0, "H")." World!";
[Link](sb);
}
}
class TaxCalculator {
static calculate(total) {
return total * .05;
}
}
[Link](50);
new [Link](50);
calculate(50);
new [Link]($50);
Note: This code won't compile, broken code sample
Reference
Code sample
Q89. Which characteristic does not apply to instances of [Link]=
uses hashcode of objects when inserted
contains unordred elements
contains unique elements
contains sorted elements
[Link] 29/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
import [Link].*;
1324
4231
1234
4321
Q91. What will this code print, assuming it is inside the main method of a class?
[Link]("hello my friends".split(" ")[0]);
my
hellomyfriends
hello
friends
Q92. You have an instance of type Map<String, Integer> named instruments
containing the following key‑value pairs: guitar=1200, cello=3000, and
drum=2000. If you add the new key‑value pair cello=4500 to the Map using the
put method, how many elements do you have in the Map when you call
[Link]()?
2
[Link] 30/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
[Link](Employee::getName).collect([Link]
());
[Link]().map(Employee::getName).collect([Link]());
Q96. This code does not compile. What needs to be changed so that it does?
return shortCode;
}
}
Add a constructor that accepts a String parameter and assigns it to the field
shortCode .
try‑catch‑finally
try‑finally‑close
try‑with‑resources
try‑catch‑close
Q98. What code should go in line 3?
class Main {
public static void main(String[] args) {
class Car {
public void accelerate() {}
}
class Lambo extends Car {
[Link] 32/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
neither
both
overloading
overriding
Q100. Which choice is the best data type for working with money in Java?
float
String
double
BigDecimal
Q101. Which statement about constructors is not ture?
A class can have multiple constructors with a different parameter list.
You can call another constructor with this or super .
A constructor does not define a return value.
Every class must explicitly define a constructor without parameters.
Q102. What language feature allows types to be parameters on classes, interfaces,
and methods in order to reuse the same code for different data types?
Regular Expressions
Reflection
Generics
Concurrency
Q103. What will be printed?
[Link] 33/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
[Link](berry + "berry");
}
}
raspberry
strawberry
blueberry
rasp
Q104. What is the value of forestCount after this code executes?
[Link]("Amazon", 30000);
[Link]("Congo", 10000);
[Link]("Daintree", 15000);
[Link]("Amazon", 40000);
3
4
2
When calling the put method, Java will throw an exception
Q105. What is a problem with this code?
import [Link];
import [Link];
import [Link];
class Main {
[Link] 34/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
interface MyInterface {
int foo(int x);
}
[Link] 35/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
interface Foo{
int x = 10;
}
10
20
null
An error will occur when compiling.
Q109. Which statement must be inserted on line 1 to print the value true?
1:
2: Optional<String> opt = [Link](val);
3: [Link]([Link]());
[Link] 36/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
Q110. What will this code print, assuming it is inside the main method of a class?
false
true
true
true
true
false
false
false
Q111. What will this code print?
[Link]( list2 );
[Link](list1);
[Two]
[One, Three]
Two
Q112. Which code checks whether the characters in two Strings,named time and
money , are the same?
[Link] 37/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
if([Link](money)){}
if(time == money){}
if(time = money){}
Q113. An _ is a serious issue thrown by the JVM that the JVM is unlikely to recover
from. An _ is an unexpected event that an application may be able to deal with in
order to continue execution.
exception,assertion
AbnormalException, AccidentalException
error, exception
exception, error
Q114. Which keyword would not be allowed here?
class Unicorn {
_____ Unicorn(){}
}
static
protected
public
void
Q115. Which OOP concept is this code an example of?
List[] myLists = {
new ArrayList<>(),
new LinkedList<>(),
new Stack<>(),
new Vector<>(),
};
composition
generics
polymorphism
[Link] 38/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
encapsulation
Explanation: switch between different implementations of the List interface
Q116. What does this code print?
String a = "bikini";
String b = new String("bikini");
String c = new String("bikini");
[Link](a == b);
[Link](b == c);
true; false
false; false
false; true
true; true
Explanation: compares the object reference. String a = "bikini";
== operator
String b = "bikini"; would result in True. Here new creates a new object, so
false. Use equals() method to compare the content.
Q117. What keyword is added to a method declaration to ensure that two threads
do not simultaneously execute it on the same object instance?
native
volatile
synchronized
lock
Java Documentation: Synchronized methods
Q118. Which is a valid type for this lambda function?
Function<Integer, Boolean>
Function<String>
Function<Integer, String>
[Link] 39/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
Function<Integer>
Explaination, Reference
Q119. What is displayed when this code is compiled and executed?
import [Link];
[Link]("Apples", 3);
[Link]("Oranges", 2);
[Link]([Link]("Apples"));
}
}
6
3
4
7
Explanation
Q120. What variable type should be declared for capitalize?
Function<String, String>
Stream<String>
String<String, String>
Map<String, String>
Explanation, Reference
Q121. Which is the correct return type for the processFunction method?
[Link] 40/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
Integer
String
Consumer
Function<Integer, String>
Explanation
Q122. What function could you use to replace slashes for dashes in a list of dates?
Explanation: replaceAll method for any List only accepts UnaryOperator to pass
every single element into it then put the result into the List again.
Q123. From which class do all other classes implicitly extend?
Object
Main
Java
Class
Explanation
Q124. How do you create and run a Thread for this class?
import [Link];
try {
[Link](5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
new CurrentDateRunnable().run();
new CurrentDateRunnable().start();
Reference
Q125. Which expression is a functional equivalent?
[Link] 42/44
6/27/2022 linkedin-skill-assessments-quizzes/[Link] at main · Ebazhanov/linkedin-skill-assessments-quizzes · GitHub
Explanation: The given code in the question will give you the output 20 as total
double pickle = 2;
int jar = pickle;
10
3
1
0
Q129. The _ runs copmpiled Java code, while the _ compiles Java files.
IDE; JRE
JDK; IDE
JRE; JDK
JDK; JRE
Reference
Q130. Which packages are part of Java Standard Edition
[Link]
[Link]
[Link]
All above
Reference
[Link] 44/44
Using '==' instead of '.equals()' for string comparison in Java leads to comparing the object references rather than their actual contents. This can result in unexpected behavior, particularly if strings are not interned or originate from different allocations, causing '==' to return false even when two strings represent the same text. Thus, using '.equals()' is essential for checking content equality, ensuring program logic behaves as intended .
Modifying a collection directly as you iterate over it using an iterator can lead to a ConcurrentModificationException in Java. This happens because the iterator detected that the collection changed in an unexpected way while iterating. To safely handle modifications, use the Iterator's own methods, such as remove(), which ensures the underlying data structure is appropriately updated without invalidating the iterator .
Lambda expressions allow for a shorter and much cleaner way to perform actions in Java using streams, by directly expressing instances of single-method interfaces (functional interfaces). This avoids the need for additional anonymous class structures. For example, in processing a list with streams to capitalize strings, a lambda can be used like this: Function<String, String> capitalize = str -> str.toUpperCase(); songTitles.stream().map(capitalize).forEach(System.out::println); This simplifies code to efficiently transform and process data while improving readability and reducing boilerplate .
The UnsupportedClassVersionError is thrown when the Java Virtual Machine (JVM) attempts to load a class with a version number that is higher than the version supported by the current JVM. This typically happens when a Java class is compiled using a newer version of the JDK but is tried to be run on an older JVM version, indicating incompatibility between the compilation and runtime environments .
The try-with-resources statement in Java is a form of the try block that declares one or more resources, which are objects that need to be closed once the operations using them are completed, such as InputStream, OutputStream, etc. The AutoCloseable interface ensures that each resource is closed at the end of the statement, automatically and without requiring an explicit call to close(). This reduces the chances of mistakes and resource leaks, enhancing code safety and readability .
In Java, casting a char to an int is permissible because a char is internally represented as an integer Unicode value. Thus, casting a char to int directly using (int) does not throw an error (e.g., char smooch = 'x'; System.out.println((int) smooch); will output 120, which is the ASCII of 'x'). However, casting errors may occur if there's an attempt to cast incompatible or large integral values outside the permissible data range, which was not specific in this scenario .
In Java, import statements of the form import packageName.subPackage.* import types based on demand and do not cause any other declaration to be shadowed. Thus, if there are similar class names in different packages, explicitly imported classes will take precedence over on-demand imports. For example, in the presence of two similarly named 'Math' classes, the class explicitly imported via 'import mypackage.Math;' overrides the on-demand import from another package, thereby accessing that class's methods .
In Java, when a key-value pair is added to a HashMap where the key already exists, the put method overwrites the current value associated with the key with the new value, rather than increasing the size of the map. For instance, if a HashMap already contains the pair 'Amazon=30000' and 'put("Amazon", 40000)' is called, the value becomes '40000', and the total number of key-value pairs remains the same .
When a class implements multiple interfaces that contain default methods with the same signature, it causes a method conflict because the class must provide an explicit implementation to resolve which method to use. This can be resolved by overriding the conflicting method in the class and specifying which interface's default method should be used using the syntax InterfaceName.super.methodName(), as shown in the following example implementation: public void method() { One.super.method(); } .
Object-Oriented Programming (OOP) fundamentally organizes a program around objects rather than functions and data. This implies that instead of thinking of solving a problem by executing a sequence of operations (as in procedural programming), OOP focuses on creating objects that represent real-world entities, which interact with one another through methods. The core aspects of OOP include encapsulation, inheritance, and polymorphism, which help in organizing complex programs by breaking them into manageable, reusable pieces .