Java questions

1. Method overloading:

public class MethodReference {
public void saySomething(){
System.out.println(“Hello, this is static method.”);
}

public static void apply(Integer i) {
System.err.println(“Integer-” + i);
}
public static void apply(String k) {
System.err.println(“String-“+k);
}
public static void main(String[] args) {

MethodReference.apply(null);

for(int i =1;i<=3;i++) {
for(int j =1;j<=i;j++) {
System.out.print(“* “);
}
System.out.println();
}
}

}

Options:
a. Compile time exception
b. Run rime exeption
c. String-null
d. Integer-null
Ans: a
2. How to find sum of an array?

Sol:

Output:

Sum of an array is: 150
Lenght of an array is: 5

3: Which of the following access specifier makes a declaration that is accessible to itself?
a. public
b. private
c. protected
d. privately
Ans: b
4: Which of the following statement is true with respect to class and members of a class?
a. It is mandatory for every class to declare a main method inside it.
b. It is mandatory to specify return type along with method definition in a class.
c. It is mandatory to initiate member variables while declaring them in a class.
d. It is mandatory to use an access specifier along with declaring statement of a member variable.
Ans: Will reveal soon.
5: What is the output of below code?
a. output is: zero
output is: one
b. Runtime error
c. output is: zero
output is: one
output is: null
output is: null
d. Compilation error
Ans: C
6: What is the output of below code?

Options:

a. output is: zero
output is: one

b. Compilation error

c. Runtime error

d. ArrayIndexOutOfBounds Exception

Ans: a

7. Write a code to find the sum of numbers between two given positions in an array.

Ans:

Ans:

Array length is: 20
n1 position is: 4
n1 position is: 14
Total sum is: 450

8. Write a code to find out first max number and then first minimum number and so on in a sorted/unsorted array?

Sol:

 

9. What is the output for below code snippet?

Options:

a. 10 20

b. 20 40

c. 10 40

d. 20 80

Ans: c

10. What is the output of below snippet?

Options:

a. First Second

b. Selenium99

c. First Second Selenium99

d. Selenium99 First Second

Ans:

b