fruit.substring(2, 5)
return?Reasoning: The substring method is accepting two arguments.
boolean b1 = true, b2 = false; int i1 = 1, i2 = 2;
(i1 | i2) == 3
i2 && b1
b1 || !b2
(i1 ^ i2) < 4
Reasoning: i2 && b1 is not allowed between int and boolean.
Reasoning: array.size() is invalid, to get size or length of array array.length can be used.
super1.print(); super2.print();
this.print();
super.print();
Interface1.print(); Interface2.print();
Reasoning: You should assign the result of trim back to the String variable. Otherwise, it is not going to work, because strings in Java are immutable.
Explanation: The answer is "123". The abs()
method evaluates to the one inside mypackage.Math class, because the import statements of the form:
import packageName.subPackage.*
is Type-Import-on-Demand Declarations, which never causes any other declaration to be shadowed.
Explanation: Compilation error at line 10 because of final methods cannot be overridden, and here message() is a final method, and also note that Non-static method message() cannot be referenced from a static context.
java Main 1 2 "3 4" 5
java Main 1 "2" "2" 5
java Main.class 1 "2" 2 5
java Main 1 "2" "3 4" 5
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.
for (Pencil pencil : pencilCase) {}
for (pencilCase.next()) {}
for (Pencil pencil : pencilCase.iterator()) {}
for (pencil in pencilCase) {}
0
names.sort(Comparator.comparing(String::toString))
Collections.sort(names)
names.sort(List.DESCENDING)
names.stream().sorted((s1, s2) -> s1.compareTo(s2)).collect(Collectors.toList())
new SimpleDateFormat("yyyy-MM-dd").format(new Date())
new Date(System.currentTimeMillis())
LocalDate.now()
Calendar.getInstance().getTime()
Explanation: LocalDate is the newest class added in java 8
int0
is divisible by 5
:boolean isDivisibleBy5 = _____
int0 / 5 ? true: false
int0 % 5 == 0
int0 % 5 != 5
Math.isDivisible(int0, 5)
Explanation: Observe the loop increment. It's not an increment, it's an assignment(post).
Note: This code won't compile, possibly broken code sample.
"nifty".getType().equals("String")
"nifty".getType() == String
"nifty".getClass().getSimpleName() == "String"
"nifty" instanceof String
Explanation: Error
is not inherited from Exception
.
[abc, 0, 2, 10]
[abc, 2, 10, 0]
[0, 10, 2, abc]
Explanation: The java.util.Arrays.asList(T... a)
returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)
Explanation: for (it.hasNext())
should be while (it.hasNext())
.
boolean healthyOrNot = isHealthy("avocado");
volatile
affect how a variable is handled?Duck waddles = new Duck();
ducks.add(waddles);
Duck duck = new Duck("Waddles");
ducks.add(waddles);
ducks.add(new Duck("Waddles"));
ducks.add(new Waddles());
UnsupportedClassVersionError
it means the code was ___
on a newer version of Java than the JRE ___
it.Explanation: final
class members are allowed to be assigned only in three places: declaration, constructor or an instance-initializer block.
abstract
classes are true?Reasoning:
new MagicPower().castSpell("expecto patronum");
MagicPower magicPower = new MagicPower();
magicPower.castSpell();
MagicPower.castSpell("expelliarmus");
new MagicPower.castSpell();
Explanation: native
is a part of JNI interface.
%
//
/
DIV
Array<Integer> numbers = new Array<Integer>(10);
Array[int] numbers = new Array[int](10);
int[] numbers = new int[10];
int numbers[] = int[10];
groucyButton.addActionListener(ActionListener listener -> System.out.println("Press me one more time..."));
groucyButton.addActionListener((event) -> System.out.println("Press me one more time..."));
groucyButton.addActionListener(new ActionListener(ActionEvent e) {() -> System.out.println("Press me one more time...");});
groucyButton.addActionListener(() -> System.out.println("Press me one more time..."));
a -> false;
(a) -> false;
String a -> false;
(String a) -> false;
"21".intValue()
String.toInt("21")
Integer.parseInt("21")
String.valueOf("21")
public String toString() { return name; }
public void println() { System.out.println(name); }
String toString() { return this.name; }
public void toString() { System.out.println(this.name); }
+
&
.
-
public static final String message
public void print2(){}
print2
method and add a semicolon.print
method.Explanation: Changing line 2 to public static final String message
raises the error message not initialized in the default constructor
.
ArrayList<String> words = new ArrayList<String>(){"Hello", "World"};
ArrayList words = Arrays.asList("Hello", "World");
ArrayList<String> words = {"Hello", "World"};
ArrayList<String> words = new ArrayList<>(Arrays.asList("Hello", "World"));
Note: This code won't compile, broken code sample.
Explanation: HashSet makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.
System.out.println("hello my friends".split(" ")[0]);
Explanation: HashMap class implements Map interface.
employees
of type List<Employee>
containing multiple entries. The Employee
type has a method getName()
that returns the employee name. Which statement properly extracts a list of employee names?employees.collect(employee -> employee.getName());
employees.filter(Employee::getName).collect(Collectors.toUnmodifiableList());
employees.stream().map(Employee::getName).collect(Collectors.toList());
employees.stream().collect((e) -> e.getName());
String
parameter and assigns it to the field shortCode
.final
keyword for the field shortCode
.shortCode
.AutoCloseable
interface are closed when it completes?int[][] array = new int[][];
int[][] array = new int[3][3];
int[][] array = new int[2][2];
int[][] array = [][];
this
or super
.forestCount
after this code executes?Function<Integer, Integer> squareLambda = (int x) -> { x * x };
Function<Integer, Integer> squareLambda = () -> { return x * x };
Function<Integer, Integer> squareLambda = x -> x * x;
Function<Integer, Integer> squareLambda = x -> return x * x;
Integer val = 15;
String val = "Sam";
String val = null;
Optional<String> val = Optional.empty();
[Two]
[One, Two, Three]
[One, Three]
Two
time
and money
, are the same?if(time <> money){}
if(time.equals(money)){}
if(time == money){}
if(time = money){}
Explanation: switch between different implementations of the List
interface.
Explanation: == operator
compares the object reference. String a = "bikini"; String b = "bikini";
would result in True. Here new creates a new object, so false. Use equals() method
to compare the content.
Function<Integer, Boolean>
Function<String>
Function<Integer, String>
Function<Integer>
Function<String, String>
Stream<String>
String<String, String>
Map<String, String>
Integer
String
Consumer
Function<Integer, String>
UnaryOperator<String> replaceSlashes = date -> date.replace("/", "-");
Function<String, String> replaceSlashes = dates -> dates.replace("-", "/");
Map<String, String> replaceSlashes = dates.replace("/", "-");
Consumer<Date> replaceSlashes = date -> date.replace("/", "-");
Explanation: replaceAll
method for any List<T> only accepts UnaryOperator<T> to pass every single element into it then put the result into the List<T> again.
Object
Main
Java
Class
Thread thread = new Thread(new CurrentDateRunnable()); thread.start();
new Thread(new CurrentDateRunnable()).join();
new CurrentDateRunnable().run();
new CurrentDateRunnable().start();
Explanation: The given code in the question will give you the output 20 as total:
Explanation:
from @yktsang01 in #3915 thread
Map because map is a key/value pair without creating new classes/objects. So can store the rainfall per month like Map<java.time.Month, Double>
.
The other options will most likely need some new class to be meaningful:
Explanation: After a thread is started, via its start()
method of the Thread class, the JVM invokes the thread's run()
method when the thread is initially executed.
Explanation: Final classes are created so the methods implemented by that class cannot be overridden. It can't be inherited. These classes are declared final
.
void accept(T t)
is method of -?Stream filter()
operates on?Stream map()
operates on?Explanation: Scope of variable Y is limited.
1. true && false
2. true && false || true
Reference //check page number 47 and example number 4.:-}