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();
}
}
}
2. How to find sum of an array?
Sol:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com; public class ArraySum { // declare and initialize a variable named sum static int sum = 0; public static void main(String[] args) { // declaring array variable and assigning int a[] = {10,20,30,40,50}; for(int i=0; i<a.length; i++){ // Here length of an array is 5 sum = sum + a[i]; // script executes 5 times to find the sum } // print the result System.out.println("Sum of an array is: "+sum); System.out.println("Lenght of an array is: "+a.length); } } |
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?
4: Which of the following statement is true with respect to class and members of a class?
5: What is the output of below code?
1 2 3 4 5 6 7 8 9 10 |
public void array(){ String[] arr = new String[4]; arr[0] = "zero"; arr[1] = "one"; for(int i=0;i<arr.length;i++){ System.out.println("output is: "+arr[i]); } } |
output is: one
output is: one
output is: null
output is: null
6: What is the output of below code?
1 2 3 4 5 6 7 8 9 10 11 12 |
// public void arrayList(){ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("zero"); arrayList.add("one"); for(int i=0;i<arrayList.size();i++){ System.out.println("output is: "+arrayList.get(i)); } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// Sum of numbers between two given indexes in an array public void arrayRandomCount2(){ // find the sum of numbers between 5th and 15th index position. int n1; int n2; int a=0,b=0; boolean b1=true; int i=0,sum=0; int[] arr = { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100}; System.out.println("Array length is: "+arr.length); n1=25; n2=75; for(i=0;i<arr.length;i++){ if(n1==(arr[i]) || n2==arr[i] ){ System.out.println("n1 position is: "+i); if(b1==true) { a=i; b1=false; } else b=i; } } for(i=a+1;i<b;i++){ sum = sum + arr[i]; } System.out.println("Total sum is: "+sum); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Array - Maximum - Minimum - Sort public void sortArray(){ int[] arr = {2,1,4,3,5,6,12,7,9,8,10,11}; int i, arrayLength; Arrays.sort(arr); arrayLength = arr.length; System.out.println("Array length is: "+arr.length); for(i=0; i<arrayLength/2; i++){ int j = arrayLength-(i+1); System.out.print(arr[j]+" "); System.out.print(arr[i]+" "); } if (arrayLength % 2 != 0) System.out.print(arr[i]); } |
9. What is the output for below code snippet?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class JavaQuestions { static int j = 20; public static void main(String argv[]) { int i = 10; JavaQuestions java = new JavaQuestions(); java.method1(i); System.out.print(i+" "); System.out.println(j); } public void method1(int x) { x = x * 2; j = j * 2; } } |
Options:
a. 10 20
b. 20 40
c. 10 40
d. 20 80
10. What is the output of below code snippet? String manipulations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Number { public static void main(String argv[]) { boolean b1 = true; if ((b1 == true) || area(true)) { System.out.println("Selenium99"); } } public static boolean area(boolean place) { if (place == true) { System.out.println("First"); } System.out.println("Second"); return true; } } |
Options:
a. First Second
b. Selenium99
c. First Second Selenium99
d. Selenium99 First Second
Ans:
b
10.1. Find the output. String manipulations.
1 2 3 4 5 |
// String manipulations String str1 = new String("selenium99"); String str2 = new String("SELENIUM99"); System.out.println(str1 = str2); |
Options:
a. false
b. true
c. SELENIUM99
d. selenium99
Ans: c