Auto Cad Question
Auto Cad Question
raw
awb
traw
method overloading
method overrunning
method overriding
method calling
(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 < array.size(); i++) {
5: System.out.print(array[i]);
6: }
7: }
8: }
123
1234
Q5. Which of the following can replace the CODE SNIPPET to make
the code below print "Hello World"?
interface Interface1 {
static void print() {
System.out.print("Hello");
}
}
interface Interface2 {
static void print() {
System.out.print("World!");
}
}
super1.print(); super2.print();
this.print();
super.print();
Interface1.print(); Interface2.print();
CD
CDE
D
"abcde"
Q7. What is the result of this code?
class Main {
public static void main (String[] args){
System.out.println(print(1));
}
static Exception print(int i){
if (i>0) {
return new Exception();
} else {
throw new RuntimeException();
}
}
}
"java.lang.Exception"
interface One {
default void method() {
System.out.println("One");
}
}
interface Two {
default void method () {
System.out.println("One");
}
}
A
B
C
D
class Main {
public static void main (String[] args) {
List list = new ArrayList();
list.add("hello");
list.add(2);
System.out.print(list.get(0) instanceof Object);
System.out.print(list.get(1) instanceof Integer);
}
}
truefalse
truetrue
falsetrue
Q10. Given the following two classes, what will be the output of
the Main class?
package mypackage;
public class Math {
public static int abs(int num){
return num < 0 ? -num : num;
}
}
package mypackage.elementary;
public class Math {
public static int abs (int num) {
return -num;
}
}
import mypackage.Math;
import mypackage.elementary.*;
class Main {
public static void main (String args[]){
System.out.println(Math.abs(123));
}
}
Lines 1 and 2 generate compiler errors due to class name
conflicts.
"-123"
"123"
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.
1: class MainClass {
2: final String message(){
3: return "Hello!";
4: }
5: }
"Hello!"
"World!"
class Main {
public static void main(String[] args) {
System.out.println(args[2]);
}
}
class Main {
public static void main(String[] args){
int a = 123451234512345;
System.out.println(a);
}
}
"123451234512345"
"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.
class Main {
public static void main (String[] args) {
String message = "Hello world!";
String newMessage = message.substring(6, 12)
+ message.substring(12, 6);
System.out.println(newMessage);
}
}
"world!!world"
"world!world!"
Q15. How do you write a foreach loop that will iterate over
ArrayList<Pencil>pencilCase?
for (pencilCase.next()) {}
System.out.print("apple".compareTo("banana"));
0
positive number
negative number
compilation error
names.sort(Comparator.comparing(String::toString))
Collections.sort(names)
names.sort(List.DESCENDING)
private
protected
no-modifier
public
new Date(System.currentTimeMillis())
LocalDate.now()
Calendar.getInstance().getTime()
Explanation: LocalDate is the newest class added in java 8
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
Math.isDivisible(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;
System.out.println("Hello World!");
}
}
}
10 times
9 times
5 times
iterative
hello
main
/* Constructor B */
Jedi(String name, String species, boolean followsTheDarkSide){}
}
Q25. What will this program print out to the console when
executed?
import java.util.LinkedList;
[5, 1, 10]
[10, 5, 1]
[1, 5, 10]
[10, 1, 5]
class Main {
public static void main(String[] args){
String message = "Hello";
for (int i = 0; i<message.length(); i++){
System.out.print(message.charAt(i+1));
}
}
}
"Hello"
"ello"
functions; actions
objects; actions
actions; functions
actions; objects
"nifty".getType().equals("String")
"nifty".getType() == String
"nifty".getClass().getSimpleName() == "String"
import java.util.*;
class Main {
public static void main(String[] args) {
List<Boolean> list = new ArrayList<>();
list.add(true);
list.add(Boolean.parseBoolean("FalSe"));
list.add(Boolean.TRUE);
System.out.print(list.size());
System.out.print(list.get(1) instanceof Boolean);
}
}
3false
2true
3true
1: class Main {
2: Object message(){
3: return "Hello!";
4: }
5: public static void main(String[] args) {
6: System.out.print(new Main().message());
7: System.out.print(new Main2().message());
8: }
9: }
10: class Main2 extends Main {
11: String message(){
12: return "World!";
13: }
14: }
Hello!Hello!
Hello!World!
another instance
field
constructor
private method
string1 == string2
string1 = string2
string1.matches(string2)
string1.equals(string2)
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;
}
System.out.println("Hello World!");
}
}
import java.util.*;
class Main {
public static void main(String[] args) {
String[] array = {"abc", "2", "10", "0"};
List<String> list = Arrays.asList(array);
Collections.sort(list);
System.out.println(Arrays.toString(array));
}
}
[abc, 0, 2, 10]
[abc, 2, 10, 0]
class Main {
public static void main(String[] args) {
String message = "Hello";
print(message);
message += "World!";
print(message);
}
static void print(String message){
System.out.print(message);
message += " ";
}
}
Hello World!
HelloHelloWorld!
Hello HelloWorld!
x
null
10
5
A
B
Iterator it = theList.iterator();
for (it.hasNext()) {
System.out.println(it.next());
}
D
theList.forEach(System.out::println);
Explanation: for (it.hasNext()) should be while (it.hasNext()).
public isHealthy("avocado")
provides, employs
imports, exports
consumes, supplies
requires, exports
non-static
static
final
private
Q42. How does the keyword volatile affect how a variable is
handled?
It will be read by only one thread at a time.
an alphanumeric character
a negative number
a positive number
a ClassCastException
ducks.add(new Duck("Waddles"));
ducks.add(new Waddles());
Q47. If you encounter UnsupportedClassVersionError
it means the code
was ___ on a newer version of Java than the JRE ___ it.
executed; interpreting
executed; compiling
compiled; executing
compiled, translating
Q48. Given this class, how would you make the code compile?
A
public TheClass() {
x += 77;
}
B
public TheClass() {
x = null;
}
C
public TheClass() {
x = 77;
}
D
4
3
5
1, 2, and 3
only 3
2 and 3
only 2
parent
super
this
new
1: int a = 1;
2: int b = 0;
3: int c = a/b;
4: System.out.println(c);
extends
implements
inherits
import
You don't have to decide the size of an ArrayList when you first
make it.
You can put more items into an ArrayList than into an array.
You don't have to decide the type of an ArrayList when you first
make it.
int pi = 3.141;
decimal pi = 3.141;
double pi = 3.141;
float pi = 3.141;
Reasoning:
MagicPower.castSpell("expelliarmus");
new MagicPower.castSpell();
constructor
instance
class
method
10 10
5 10
10 5
55
try {
System.out.println("Hello World");
} catch (Exception e) {
System.out.println("e");
} catch (ArithmeticException e) {
System.out.println("e");
} finally {
System.out.println("!");
}
Hello World
Hello World!
finally
native
interface
unsigned
Q62. Which operator would you use to find the remainder after
division?
%
//
/
DIV
Reference
Reference
groucyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
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...");});
Observer, Observable
Collector, Builder
Reference
Reference
uses-a
is-a
has-a
was-a
Reference
Reference
Q71. Using Java's Reflection API, you can use _ to get the name of
a class and _ to retrieve an array of its methods.
this.getClass().getSimpleName();
this.getClass().getDeclaredMethods()
this.getName(); this.getMethods()
Reflection.getName(this); Reflection.getMethods(this)
Reflection.getClass(this).getName();
Reflection.getClass(this).getMethods()
a -> false;
public
protected
nonmodifier
private
private
non-static
final
static
"21".intValue()
String.toInt("21")
Integer.parseInt("21")
String.valueOf("21")
Q76. What method should be added to the Duck class to print the
name Moby?
Duck(String name) {
this.name = name;
}
+
&
.
-
Reference
two
four
three
five
p
r
e
i
pass by reference
pass by occurrence
pass by value
API call
5
8
1
3
import java.util.*;
class Main {
public static void main(String[] args) {
String[] array = new String[]{"A", "B", "C"};
List<String> list1 = Arrays.asList(array);
List<String> list2 = new ArrayList<>(Arrays.asList(array));
List<String> list3 = new ArrayList<>(Arrays.asList("A", new
String("B"), "C"));
System.out.print(list1.equals(list2));
System.out.print(list1.equals(list3));
}
}
falsefalse
truetrue
falsetrue
truefalse
class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("hello");
sb.deleteCharAt(0).insert(0, "H")." World!";
System.out.println(sb);
}
}
"HelloWorld!"
"hello"
????
class TaxCalculator {
static calculate(total) {
return total * .05;
}
}
TaxCalculator.calculate(50);
new TaxCalculator.calculate(50);
calculate(50);
new TaxCalculator.calculate($50);
Reference
Code sample
Reference
1324
4231
1234
4321
Q91. What will this code print, assuming it is inside the main
method of a class?
my
hellomyfriends
hello
friends
2
3
Q93. Which class acts as root class for Java Exception hierarchy?
Clonable
Throwable
Object
Serializable
java.util.Vector
java.util.ArrayList
java.util.HashSet
java.util.HashMap
employees.filter(Employee::getName).collect(Collectors.toUnmodifi
ableList());
employees.stream().map(Employee::getName).collect(Collectors.toLi
st());
try-finally-close
try-with-resources
try-catch-close
class Main {
public static void main(String[] args) {
class Car {
public void accelerate() {}
}
class Lambo extends Car {
public void accelerate(int speedLimit) {}
public void accelerate() {}
}
neither
both
overloading
overriding
Q100. Which choice is the best data type for working with money
in Java?
float
String
double
BigDecimal
Regular Expressions
Reflection
Generics
Concurrency
raspberry
strawberry
blueberry
rasp
forestSpecies.put("Amazon", 30000);
forestSpecies.put("Congo", 10000);
forestSpecies.put("Daintree", 15000);
forestSpecies.put("Amazon", 40000);
3
4
2
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Main {
interface MyInterface {
int foo(int x);
}
A
B
public class MyClass implements MyInterface {
// ....
public double foo(int x){
return x * 100;
}
}
C
D
interface Foo{
int x = 10;
}
10
20
null
1:
2: Optional<String> opt = Optional.of(val);
3: System.out.println(opt.isPresent());
Q110. What will this code print, assuming it is inside the main
method of a class?
false
true
true
true
true
false
false
false
list1.remove( list2 );
System.out.println(list1);
[Two]
[One, Three]
Two
Q112. Which code checks whether the characters in two
Strings,named time and money, are the same?
if(time <> money){}
if(time.equals(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
class Unicorn {
_____ Unicorn(){}
}
static
protected
public
void
List[] myLists = {
new ArrayList<>(),
new LinkedList<>(),
new Stack<>(),
new Vector<>(),
};
composition
generics
polymorphism
encapsulation
Explanation: switch between different implementations of
the List interface
String a = "bikini";
String b = new String("bikini");
String c = new String("bikini");
System.out.println(a == b);
System.out.println(b == c);
true; false
false; false
false; true
true; true
native
volatile
synchronized
lock
Function<Integer, Boolean>
Function<String>
Function<Integer, String>
Function<Integer>
Explaination, Reference
import java.util.HashMap;
pantry.put("Apples", 3);
pantry.put("Oranges", 2);
System.out.println(pantry.get("Apples"));
}
}
6
3
4
7
Explanation
Function<String, String>
Stream<String>
String<String, String>
Map<String, String>
Explanation, Reference
String
Consumer
Function<Integer, String>
Explanation
Q122. What function could you use to replace slashes for dashes
in a list of dates?
Object
Main
Java
Class
Explanation
Q124. How do you create and run a Thread for this class?
import java.util.date;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
Thread thread = new Thread(new CurrentDateRunnable());
thread.start();
new CurrentDateRunnable().run();
new CurrentDateRunnable().start();
Reference
A
B
C
D
out
err
in
double pickle = 2;
int jar = pickle;
Use the new keyword to create a new Integer from pickle before
assigning it to jar.
10
3
1
0
Q129. The _ runs copmpiled Java code, while the _ compiles Java
files.
IDE; JRE
JDK; IDE
JRE; JDK
JDK; JRE
Reference
java.net
java.util
java.lang
All above