Q1. Given the string "strawberries" saved in a variable called fruit, what would fruit.substring(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).
- method overloading
- method overrunning
- method overriding
- method calling
boolean b1 = true, b2 = false; int i1 = 1, i2 = 2;
-
(i1 | i2) == 3
-
i2 && b1
-
b1 || !b2
-
(i1 ^ i2) < 4
- constructor
- another instance
- field
- private method
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: }
- It will not compile because of line 4.
- It will not compile because of line 3.
- 123
- 1234
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();
String str = "abcde";
str.trim();
str.toUpperCase();
str.substring(3, 4);
System.out.println(str);
- CD
- CDE
- D
- "abcde"
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();
}
}
}
- It will show a stack trace with a runtime exception.
- "java.lang.Exception"
- It will run and throw an exception.
- It will not compile.
interface One {
default void method() {
System.out.println(""One"");
}
}
interface Two {
default void method () {
System.out.println(""One"");
}
}
- A
class Three implements One, Two {
public void method() {
super.One.method();
}
}
- B
class Three implements One, Two {
public void method() {
One.method();
}
}
- C
class Three implements One, Two {
}
- D
class Three implements One, Two {
public void method() {
One.super.method();
}
}
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);
}
}
- The code does not compile.
- truefalse
- truetrue
- falsetrue
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"
- It will throw an exception on line 5.
- "123"
Explanation: The answer is "123". The abs()
method evaluates to the one inside mypackage.Math class.
1: class MainClass {
2: final String message(){
3: return "Hello!";
4: }
5: }
6: class Main extends MainClass {
7: public static void main(String[] args) {
8: System.out.println(message());
9: }
10: String message(){
11: return "World!";
12: }
13: }
- It will not compile because of line 10.
- "Hello!"
- It will not compile because of line 2.
- "World!"
Explanation: Non-static method message() cannot be referenced from a static context.
class Main {
public static void main(String[] args) {
System.out.println(args[2]);
}
}
-
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
class Main {
public static void main(String[] args){
int a = 123451234512345;
System.out.println(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.
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);
}
}
- The code does not compile.
- A runtime exception is thrown.
- "world!!world"
- "world!world!"
String m = "Hello world!";
String n = m.substring(6,12) + m.substring(12,6);
System.out.println(n);
-
for (Pencil pencil : pencilCase) {}
-
for (pencilCase.next()) {}
-
for (Pencil pencil : pencilCase.iterator()) {}
-
for (pencil in pencilCase) {}
- Object-oriented programming (OOP) is a programming language model that organizes software design around (objects), rather than (actions).
-
"schwifty".getType() == String
-
"schwifty".getClass().getSimpleName() == "String"
-
"schwifty".getType().equals("String")
-
"schwifty" instanceof String
System.out.print("apple".compareTo("banana"));
-
0
- positive number
- negative number
- compilation error
Q20. You have an ArrayList of names that you want to sort alphabetically. Which approach would NOT work?
-
names.sort(Comparator.comparing(String::toString))
-
Collections.sort(names)
-
names.sort(List.DESCENDING)
-
names.stream().sorted((s1, s2) -> s1.compareTo(s2)).collect(Collectors.toList())
Q21. By implementing encapsulation, you cannot directly access the class's _ properties unless you are writing code inside the class itself.
- private
- protected
- no-modifier
- public
-
new SimpleDateFormat("yyyy-MM-dd").format(new Date())
-
new Date(System.currentTimeMillis())
-
LocalDate.now()
-
Calendar.getInstance().getTime()
Explanation: LocalDate is newest class added in java 8
boolean isDivisibleBy5 = _____
-
int0 / 5 ? true: false
-
int0 % 5 == 0
-
int0 % 5 != 5
-
Math.isDivisible(int0, 5)
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
- infinite number of times
Reason : Observe the loop increment. It's not an increment, it's an assignment(post).
- iterative
- hello
- main
try{
System.out.print("Hello World");
}catch(Exception e){
System.out.println("e");
}catch(ArithmeticException e){
System.out.println("e");
}finally{
System.out.println("!")
}
- It will throw a runtime exception
- It will not compile
- Hello World!
- Hello World
- An anonymous class may specify an abstract base class as its base type.
- An anonymous class does not require a zero-argument constructor.
- An anonymous class may specify an interface as its base type.
- An anonymous class may specify both an abstract class and interface as base types.
import java.util.LinkedList;
public class Main {
public static void main(String[] args){
LinkedList<Integer> list = new LinkedList<>();
list.add(5);
list.add(1);
list.add(10);
System.out.println(list);
}
}
- [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"
- A runtime exception is thrown.
- The code does not compile.
- "ello"
Q30. 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
-
"nifty".getType().equals("String")
-
"nifty".getType() == String
-
"nifty".getClass().getSimpleName() == "String"
-
"nifty" instanceof 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);
}
}
- A runtime exception is thrown.
- 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: }
- It will not compile because of line 7.
- Hello!Hello!
- Hello!World!
- It will not compile because of line 11.
- another instance
- field
- constructor
- private method
Q35. Which is the most reliable expression for testing whether the values of two string variables are the same?
- string1 == string2
- string1 = string2
- string1.matches(string2)
- string1.equals(string2)
public static void main(String[] args) {
try {
System.out.println("A");
badMethod();
System.out.println("B");
} catch (Exception ex) {
System.out.println("C");
} finally {
System.out.println("D");
}
}
public static void badMethod() {
throw new Error();
}
- A, B, and D
- A, C, and D
- C and D
- A and D
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!");
}
}
- It will throw a runtime exception.
- It will not compile.
- It will print "Hello World!" three times.
- It will run forever.
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]
- The code does not compile.
- [abc, 2, 10, 0]
- [0, 10, 2, abc]
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 Hello World!
- Hello HelloWorld!
public class Main {
public static void main(String[] args) {
int x = 5;
x = 10;
System.out.println(x);
}
}
- x
- null
- 10
- 5
- A
for (int i = 0; i < theList.size(); i++) {
System.out.println(theList.get(i));
}
- B
for (Object object : theList) {
System.out.println(object);
}
- C
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())
.
boolean healthyOrNot = isHealthy("avocado");
- public void isHealthy(String avocado)
- boolean isHealthy(String string)
- public isHealthy("avocado")
- private String isHealthy(String food)
- provides, employs
- imports, exports
- consumes, supplies
- requires, exports
- non-static
- static
- final
- private
- It will be read by only one thread at a time.
- It will be stored on the hard drive.
- It will never be cached by the CPU.
- It will be preferentially garbage collected.
char smooch = 'x';
System.out.println((int) smooch);
- an alphanumeric character
- a negative number
- a positive number
- a ClassCastException
- A file that needs to be opened cannot be found.
- A network connection has been lost in the middle of communications.
- Your code has used up all available memory.
- The object you are using has not been instantiated.
public class Nosey {
int age;
public static void main(String[] args) {
System.out.println("Your age is: " + age);
}
}
- Make age static.
- Make age global.
- Make age public.
- Initialize age to a number.
public class Duck {
private String name;
Duck(String name) {}
}
-
Duck waddles = new Duck();
ducks.add(waddles);
-
Duck duck = new Duck("Waddles");
ducks.add(waddles);
-
ducks.add(new Duck("Waddles"));
-
ducks.add(new Waddles());
Q50. 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
public class TheClass {
private final int x;
}
- A
public TheClass() {
x += 77;
}
- B
public TheClass() {
x = null;
}
- C
public TheClass() {
x = 77;
}
- D
private void setX(int x) {
this.x = x;
}
public TheClass() {
setX(77);
}
public class Solution {
public static void main(String[] args) {
for (int i = 44; i > 40; i--) {
System.out.println("f");
}
}
}
- 4
- 3
- 5
- A Runtime exception will be thrown
1. They can be instantiated.
2. They allow member variables and methods to be inherited by subclasses.
3. They can contain constructors.
- 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);
- It will throw an ArithmeticException.
- It will run and output 0.
- It will not compile because of line 3.
- It will run and output infinity.
Q56. Normally, to access a static member of a class such as Math.PI, you would need to specify the class "Math". What would be the best way to allow you to use simply "PI" in your code?
- Add a static import.
- Declare local copies of the constant in your code.
- This cannot be done. You must always qualify references to static members with the class form which they came from.
- Put the static members in an interface and inherit from that interface.
- 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.
- ArrayLists can hold more kinds of objects than arrays.
- 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;
public class MagicPower {
void castSpell(String spell) {}
}
-
new MagicPower().castSpell("expecto patronum")
-
MagicPower magicPower = new MagicPower();
magicPower.castSpell();
-
MagicPower.castSpell("expelliarmus");
-
new MagicPower.castSpell();
Q61. What language construct serves as a blueprint containing an object's properties and functionality?
- constructor
- instance
- class
- method
public static void main(String[] args) {
int x=5,y=10;
swapsies(x,y);
System.out.println(x+"="+y);
}
static void swapsies(int a, int b) {
int temp=a;
a=b;
b=temp;
}
- 10=10
- 5=10
- 10=5
- 5=5
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
- It will not compile because the second catch statement is unreachable
- Hello World!
- It will throw runtime exception
- finally
- native
- interface
- unsigned
-
%
-
//
-
/
-
DIV
- Overridden methods of the parent class cannot be reused.
- 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.
-
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(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Press me one more time..");
}
});
-
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..."));
- Observer, Observable
- Collector, Builder
- Filter, Map, Reduce
- Consumer, Predicate, Supplier