Core Java Code Snippets
Core Java Code Snippets
Core Java Code Snippets
a) At run time
b) At compile time
c) At coding time
d) At execution time
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
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. }
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. }
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. }
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. }
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. }
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. }
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 };
27. Which Of The Following Is The Result Of The Following Java Code?
class TestApp {
class TestApp {
int test() {
int index = 1;
return index;
}
}
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