Core Java Code Snippets

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

1. When is method overloading determined?

a) At run time
b) At compile time
c) At coding time
d) At execution time

2. When Overloading does not occur?


a) More than one method with same name but different method
signature and different number or type of parameters
b) More than one method with same name, same signature but
different number of signature
c) More than one method with same name, same signature, same
number of parameters but different type
d) More than one method with same name, same number of
parameters and type but different signature

3. Which concept of Java is a way of converting real world objects in


terms of class?
a) Polymorphism
b) Encapsulation
c) Abstraction
d) Inheritance

4. Which component is responsible for converting bytecode into


machine specific code?
a) JVM
b) JDK
c) JIT
d) JRE

5. What is the process of defining a method in a subclass having same


name & type signature as a method in its superclass?
a) Method overloading
b) Method overriding
c) Method hiding
d) None of the mentioned

6. Which of these keywords can be used to prevent Method


overriding?
a) static
b) constant
c) protected
d) final

7.What will be the output of the following Java program?


1. class Abc
2. {
3. public static void main(String[]args)
4. {
5. String[] elements = { "for", "tea",
"too" };
6. String first = (elements.length >
0) ? elements[0]: null;
7. }
8. }

8.What will be the output of the following Java program?


1. class A
2. {
3. int i;
4. public void display()
5. {
6. System.out.println(i);
7. }
8. }
9. class B extends A
10. {
11. int j;
12. public void display()
13. {
14. System.out.println(j);
15. }
16. }
17. class Dynamic_dispatch
18. {
19. public static void main(String
args[])
20. {
21. B obj2 = new B();
22. obj2.i = 1;
23. obj2.j = 2;
24. A r;
25. r = obj2;
26. r.display();
27. }
28. }
9. Which of the following are legal lines of Java code?
1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;
a) 1 and 2
b) 2 and 3
c) 3 and 4
d) All statements are correct

10.  What will be the output of the following Java code?


1. class increment {
2. public static void main(String args[])
3. {
4. int g = 3;
5. System.out.print(++g * 8);
6. }
7. }

11. What will be the output of the following Java program?


1. class array_output {
2. public static void main(String args[])
3. {
4. char array_variable [] = new char[10];
5. for (int i = 0; i < 10; ++i) {
6. array_variable[i] = 'i';
7. System.out.print(array_variable[i]
+ "" );
8. i++;
9. }
10. }
11. }

12.What will be the output of the following Java code?

1. class access
2. {
3. public int x;
4. private int y;
5. void cal(int a, int b)
6. {
7. x = a + 1;
8. y = b;
9. }
10. }
11. public class access_specifier
12. {
13. public static void main(String args[])
14. {
15. access obj = new access();
16. obj.cal(2, 3);
17. System.out.println(obj.x + " " +
obj.y);
18. }
19. }

13. How many copies of static and class variables are created when 10 objects
are created of a class?
a) 1, 10
b) 10, 10
c) 10, 1
d) 1, 1

14.How can a protected modifier be accessed?


a) accessible only within the class
b) accessible only within package
c) accessible within package and outside the package but through inheritance
only
d) accessible by all

15. What will be the output of the following Java program?

1. class access
2. {
3. public int x;
4. static int y;
5. void cal(int a, int b)
6. {
7. x += a ;
8. y += b;
9. }
10. }
11. class static_specifier
12. {
13. public static void main(String args[])
14. {
15. access obj1 = new access();
16. access obj2 = new access();
17. obj1.x = 0;
18. obj1.y = 0;
19. obj1.cal(1, 2);
20. obj2.x = 0;
21. obj2.cal(2, 3);
22. System.out.println(obj1.x + " " + obj2.y);
23. }
24. }

16.What will be the output of the following Java program?


1. class access
2. {
3. static int x;
4. void increment()
5. {
6. x++;
7. }
8. }
9. class static_use
10. {
11. public static void main(String args[])
12. {
13. access obj1 = new access();
14. access obj2 = new access();
15. obj1.x = 0;
16. obj1.increment();
17. obj2.increment();
18. System.out.println(obj1.x + " " + obj2.x);
19. }
20. }

17.What will be the output of the following Java program?

1. class Output
2. {
3. public static void main(String args[])
4. {
5. int arr[] = {1, 2, 3, 4, 5};
6. for ( int i = 0; i < arr.length - 2; ++i)
7. System.out.println(arr[i] + " ");
8. }
9. }

18. What will be the output of the following Java program?


1. class San
2. {
3. public void m1 (int i,float f)
4. {
5. System.out.println(" int float method");
6. }
7.  
8. public void m1(float f,int i);
9. {
10. System.out.println("float int method");
11. }
12. 
13. public static void main(String[]args)
14. {
15. San s=new San();
16. s.m1(20,20);
17. }
18.}

 
19.What will be the output of the following Java program, Command line
exceution is done as – “java Output This is a command Line”?
1. class Output
2. {
3. public static void main(String args[])
4. {
5. System.out.print("(int)args[2] * 2");
6. }
7. }

20.How many arguments can be passed to main()?


a) Infinite
b) Only 1
c) System Dependent
d) None of the mentioned.

21. What will be the output of the following Java program?

1. class output
2. {
3. public static void main(String args[])
4. {
5. String a = "hello i love java";
6. System.out.println(a.indexOf('e')+"
"+a.indexOf('a')+" "+a.lastIndexOf('l')+"
"+a.lastIndexOf('v'));
7. }
8. }

22.What will be the output of the following Java program?

1. class output
2. {
3. public static void main(String args[])
4. {
5. char c[]={'A', '1', 'b' ,' ' ,'a' , '0'};
6. for (int i = 0; i < 5; ++i)
7. {
8. i++;
9. if(Character.isDigit(c[i]))
10. System.out.println(c[i]+" is a digit");
11. if(Character.isWhitespace(c[i]))
12. System.out.println(c[i]+" is a Whitespace
character");
13. if(Character.isUpperCase(c[i]))
14. System.out.println(c[i]+" is an Upper case
Letter");
15. if(Character.isLowerCase(c[i]))
16. System.out.println(c[i]+" is a lower case
Letter");
17. i++;
18. }
19. }

23. What will be the output of the following Java program?

1. class String_demo
2. {
3. public static void main(String args[])
4. {
5. char chars[] = {'a', 'b', 'c'};
6. String s = new String(chars);
7. String s1 = "abcd";
8. int len1 = s1.length();
9. int len2 = s.length();
10. System.out.println(len1 + " " +
len2);
11. }
12. }
25. What will be the output of the following Java code?
26. class output
27. {
28. public static void main(String args[])
29. {
30. String s1 = "Hello i love java";
31. String s2 = new String(s1);
32. System.out.println((s1 == s2) + " " +
s1.equals(s2));
33. }
34. }

24. In the following Java code, which code fragment should be inserted at line
3 so that the output will be: “123abc 123abc”?

1. class output
2. {
3. public static void main(String args[])
4. {
5. String chars[] = {"a", "b", "c", "a", "c"};
6. for (int i = 0; i < chars.length; ++i)
7. for (int j = i + 1; j < chars.length; ++j)
8. if(chars[i].compareTo(chars[j]) == 0)
9. System.out.print(chars[j]);
10. }
11. }

25. What will be the output of the following Java program?

1. class output
2. {
3. public static void main(String args[])
4. { String s = "Hello World";
5. int i = s.indexOf('o');
6. int j = s.lastIndexOf('l');
7. System.out.print(i + " " + j);
8.  
9. }
10. }
26.  Which Of The Following Values Would The Below Java Coding Snippet Print
In Results?

class TestApp {
int i[] = { 0 };

public static void main(String args[]) {


int i[] = { 1 };
alter(i);
System.out.println(i[0]);
}

public static void alter(int i[]) {


int j[] = { 2 };
i = j;
}
}

27. Which Of The Following Is The Result Of The Following Java Code?

class TestApp {

String args[] = { "1", "2" };

public static void main(String args[]) {


if (args.length > 0)
System.out.println(args.length);
}
}

29.What is the output of the following code ?

class TestApp {

static int index = 0;


public static void main(String args[]) {
System.out.println(test());
}

int test() {
int index = 1;
return index;
}
}

30.Write the output of the following code ?

class superClass
{
    final public int calc(int a, int b)
    {
        return 0;
    }
}
class subClass extends superClass
{
    public int calc(int a, int b)
    {
        return 1;
    }
}
public class Gfg
{
    public static void main(String args[])
    {
        subClass get = new subClass();
        System.out.println("x = " + get.calc(0, 1));
    }
}

1b ,2d, 3d? ,4a, 5b, 6d, 7for, 8 2, 9 b?, 10 32, 11 i ii iii iiii iiiii iiiiii iiiii iiiii iiiiiii ,
12 3 null, private variable ; 13 d,14 c, 15 1 2, 16 0?, 17 4, 18 int float
method20 20 , float int method 20 20, 19? ,20 c, 21 1 ,11, 7,12; 22 each
iteration, 23 abcd, abc, 25 ? , 24 3, 25 4 3; 26 2, 27 1; 29 1; 30 null; 29m 40s

You might also like