Java Questions For Badejo
Java Questions For Badejo
Java Questions For Badejo
This Section of our 1000+ Java MCQs focuses on Integer and Floating Datatypes of Java
Programming Language.
1.What is the range of data type short in Java?
a)-128to127
b)-32768to32767
c)-2147483648 to 2147483647
d) None of the mentioned
View Answer
Answer:b
Explanation:Short occupies 16 bits in memory. Its range is from -32768 to 32767.
2. What is the range of data type byte in Java?
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
d) None of the mentioned
View Answer
Answer:a
Explanation:Byte occupies 8 bits in memory. Its range is from -128 to 127.
3. 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.
View Answer
Answer: d
Explanation:Statements (1), (2), (3), and (4) are correct. (1) is correct because when a
floating-point number (a double in this case) is cast to an int, it simply loses the digits
after the decimal.(2) and (4) are correct because a long can be cast into a byte. If the long
is over 127, it loses its most significant (leftmost) bits.(3) actually works, even though a
cast is not necessary, because a long can store a byte.
4. An expression involving byte, int, and literal numbers is promoted to which of these?
a) int
b) long
c) byte
d) float
View Answer
Answer: a
Explanation:An expression involving bytes, ints, shorts, literal numbers, the entire
expression is promoted to int before any calculation is done.
5. Which of these literals can be contained in a data type float variable?
a) 1.7e-308
b) 3.4e-038
c) 1.7e+308
d) 3.4e-050
View Answer
Answer: b
Explanation:Range of data type float is 3.4e-038 to 3.4e+308.
6. Which data type value is returned by all transcendental math functions?
a) int
b) float
c) double
d) long
View Answer
Answer:c
Explanation:None.
7. What is the output of this program?
1. class average {
2. public static void main(String args[])
3. {
4. double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
5. double result;
6. result = 0;
7. for (int i = 0; i < 6; ++i)
8. result = result + num[i];
9. System.out.print(result/6);
10.
11. }
12. }
a) 16.34
b) 16.566666644
c) 16.46666666666667
d) 16.46666666666666
View Answer
Answer:c
Explanation:None.
16.46666666666667
8. What is the output of this program?
1. class conversion {
2. public static void main(String args[])
3. {
4. double a = 295.04;
5. int b = 300;
6. byte c = (byte) a;
7. byte d = (byte) b;
8. System.out.println(c + " " + d);
9. }
10. }
a) 38 43
b) 39 44
c) 295 300
d) 295.04 300
View Answer
Answer:b
Explanation:Type casting a larger variable into a smaller variable results in modulo of
larger variable by range of smaller variable. b contains 300 which is larger than byte’s
range i:e -128 to 127 hence d contains 300 modulo 256 i:e 44.
output:
$ javac conversion.java
$ java conversion
39 44
9. What is the output of this program?
1. class increment {
2. public static void main(String args[])
3. {
4. int g = 3;
5. System.out.print(++g * 8);
6. }
7. }
a) 25
b) 24
c) 32
d) 33
View Answer
Answer:c
Explanation:Operator ++ has more preference than *, thus g becomes 4 and when
multiplied by 8 gives 32.
a) 301.5656
b) 301
c) 301.56
d) 301.56560000
View Answer
Answer:a
Explanation:None.
a) i i i i i
b) 0 1 2 3 4
c) i j k l m
d) None of the mentioned
View Answer
Answer:a
Explanation:None.
7. What is the output of this program?
1. class mainclass {
2. public static void main(String args[])
3. {
4. char a = 'A';
5. a++;
6. System.out.print((int)a);
7. }
8. }
a) 66
b) 67
c) 65
d) 64
View Answer
Answer:a
Explanation: ASCII value of ‘A’ is 65, on using ++ operator character value increments by
one.
8. What is the output of this program?
1. class mainclass {
2. public static void main(String args[])
3. {
4. boolean var1 = true;
5. boolean var2 = false;
6. if (var1)
7. System.out.println(var1);
8. else
9. System.out.println(var2);
10. }
11. }
a) 0
b) 1
c) true
d) false
View Answer
Answer:c
Explanation:None.
a) 0
b) 1
c) true
d) false
View Answer
Answer:d
Explanation: boolean ‘&’ operator always returns true or false. var1 is defined true and
var2 is defined false hence their ‘&’ operator result is false.
10. What is the output of this program?
1. class asciicodes {
2. public static void main(String args[])
3. {
4. char var1 = 'A';
5. char var2 = 'a';
6. System.out.println((int)var1 + " " + (int)var2);
7. }
8. }
a) 162
b) 65 97
c) 67 95
d) 66 98
View Answer
Answer:b
Explanation:ASCII code for ‘A’ is 65 and for ‘a’ is 97.
a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
View Answer
Answer:a
Explanation:When an array is declared using new operator then all of its elements are
initialized to 0 automatically. for loop body is executed 5 times as whenever controls
comes in the loop i value is incremented twice, first by i++ in body of loop then by ++i in
increment condition of for loop.
7. What is the output of this program?
1. class multidimention_array {
2. public static void main(String args[])
3. {
4. int arr[][] = new int[3][];
5. arr[0] = new int[1];
6. arr[1] = new int[2];
7. arr[2] = new int[3];
8. int sum = 0;
9. for (int i = 0; i < 3; ++i)
10. for (int j = 0; j < i + 1; ++j)
11. arr[i][j] = j + 1;
12. for (int i = 0; i < 3; ++i)
13. for (int j = 0; j < i + 1; ++j)
14. sum + = arr[i][j];
15. System.out.print(sum);
16. }
17. }
a) 11
b) 10
c) 13
d) 14
View Answer
Answer:b
Explanation:arr[][] is a 2D array, array has been allotted memory in parts. 1st row contains
1 element, 2nd row contains 2 elements and 3rd row contains 3 elements. each element of
array is given i + j value in loop. sum contains addition of all the elements of the array.
a) 3
b) 0
c) 6
d) 1
View Answer
Answer:d
Explanation:Array arr contains 10 elements. n contains 6 thus in next line n is given value
2 printing arr[2]/2 i:e 2/2 = 1.
9. What is the output of this 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. }
9. }
10. }
a) 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 3 4 5 6 7 8 9 10
c) i j k l m n o p q r
d) i i i i i i i i i i
View Answer
Answer:d
Explanation:None.
10. What is the output of this program?
1. class array_output {
2. public static void main(String args[])
3. {
4. int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8,
9}};
5. int sum = 0;
6. for (int i = 0; i < 3; ++i)
7. for (int j = 0; j < 3 ; ++j)
8. sum = sum + array_variable[i][j];
9. System.out.print(sum / 5);
10. }
11. }
a) 8
b) 9
c) 10
d) 11
View Answer
Answer:b
Explanation:None.
a) E U
b) U E
c) V E
d) U F
View Answer
Answer:a
Explanation:Operator ++ increments the value of character by 1. c1 and c2 are given
values D and 84, when we use ++ operator their values increments by 1, c1 and c2
becomes E and U respectively.
output:
EU
7. What is the output of this program?
1. class conversion {
2. public static void main(String args[])
3. {
4. double a = 295.04;
5. int b = 300;
6. byte c = (byte) a;
7. byte d = (byte) b;
8. System.out.println(c + " " + d);
9. }
10. }
a) 38 43
b) 39 44
c) 295 300
d) 295.04 300
View Answer
Answer:b
Explanation:Type casting a larger variable into a smaller variable results in modulo of
larger variable by range of smaller variable. b contains 300 which is larger than byte’s
range i:e -128 to 127 hence d contains 300 modulo 256 i:e 44.
output:
39 44
8. What is the output of this program?
1. class A {
2. final public int calculate(int a, int b) { return 1; }
3. }
4. class B extends A {
5. public int calculate(int a, int b) { return 2; }
6. }
7. public class output {
8. public static void main(String args[])
9. {
10. B object = new B();
11. System.out.print("b is " + b.calculate(0, 1));
12. }
13. }
a) b is : 2
b) b is : 1
c) Compilation Error.
d) An exception is thrown at runtime.
View Answer
Answer:c
Explanation:The code does not compile because the method calculate() in class A is final
and so cannot be overridden by method of class b.
9. What is the output of this program?
1. class main_arguments {
2. public static void main(String [] args)
3. {
4. String [][] argument = new String[2][2];
5. int x;
6. argument[0] = args;
7. x = argument[0].length;
8. for (int y = 0; y < x; y++)
9. System.out.print(" " + argument[0][y]);
10. }
11. }
a) 1 1
b) 1 0
c) 1 0 3
d) 1 2 3
View Answer
Answer:d
Explanation:In argument[0] = args;, the reference variable arg[0], which was referring to
an array with two elements, is reassigned to an array (args) with three elements.
Output:
123
10. What is the output of this program?
1. class c {
2. public void main( String[] args )
3. {
4. System.out.println( "Hello" + args[0] );
5. }
6. }
a) Hello c
b) Hello
c) Hello world
d) Runtime Error.
View Answer
Answer:d
Explanation:A runtime error will occur owning to the main method of the code fragment
not being declared static.
a) I
b) L
c) K
d) E
View Answer
Answer: a
Explanation: charAt() is a method of class String which gives the character specified by
the index. obj.charAt(3) gives 4th character i:e I.
output:
I
8. What is the output of this program?
1. class string_class {
2. public static void main(String args[])
3. {
4. String obj = "I LIKE JAVA";
5. System.out.println(obj.length());
6. }
7. }
a) 9
b) 10
c) 11
d) 12
View Answer
Answer: c
Explanation: None.
output:
11
9. What is the output of this program?
1. class string_class {
2. public static void main(String args[])
3. {
4. String obj = "hello";
5. String obj1 = "world";
6. String obj2 = obj;
7. obj2 = " world";
8. System.out.println(obj + " " + obj2);
9. }
10. }
a) hello hello
b) world world
c) hello world
d) world hello
View Answer
Answer: c
Explanation: None.
output:
hello world
10. What is the output of this program?
1. class string_class {
2. public static void main(String args[])
3. {
4. String obj = "hello";
5. String obj1 = "world";
6. String obj2 = "hello";
7. System.out.println(obj.equals(obj1) + " " +
obj.equals(obj2));
8. }
9. }
a) false false
b) true true
c) true false
d) false true
View Answer
Answer: d
Explanation: equals() is method of class String, it is used to check equality of two String
objects, if they are equal, true is retuned else false.
output:
false true
a) 0
b) 1
c) 2
d) Compilation Error
View Answer
Answer: c
Explanation: class A & class B both contain display() method, class B inherits class A,
when display() method is called by object of class B, display() method of class B is
executed rather than that of Class A.
output:
2
7. What is the output of this program?
1. class A {
2. int i;
3. }
4. class B extends A {
5. int j;
6. void display() {
7. super.i = j + 1;
8. System.out.println(j + " " + i);
9. }
10. }
11. class inheritance {
12. public static void main(String args[])
13. {
14. B obj = new B();
15. obj.i=1;
16. obj.j=2;
17. obj.display();
18. }
19. }
a) 2 2
b) 3 3
c) 2 3
d) 3 2
View Answer
Answer: c
Explanation: None
output:
23
8. What is the output of this program?
1. class A {
2. public int i;
3. private int j;
4. }
5. class B extends A {
6. void display() {
7. super.j = super.i + 1;
8. System.out.println(super.i + " " + super.j);
9. }
10. }
11. class inheritance {
12. public static void main(String args[])
13. {
14. B obj = new B();
15. obj.i=1;
16. obj.j=2;
17. obj.display();
18. }
19. }
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
View Answer
Answer: d
Explanation: class contains a private member variable j, this cannot be inherited by
subclass B and does not have access to it.
output:
The field A.j is not visible
9. What is the output of this program?
1. class A {
2. public int i;
3. public int j;
4. A() {
5. i = 1;
6. j = 2;
7. }
8. }
9. class B extends A {
10. int a;
11. B() {
12. super();
13. }
14. }
15. class super_use {
16. public static void main(String args[])
17. {
18. B obj = new B();
19. System.out.println(obj.i + " " + obj.j)
20. }
21. }
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
View Answer
Answer: a
Explanation: Keyword super is used to call constructor of class A by constructor of class
B. Constructor of a initializes i & j to 1 & 2 respectively.
output:
12
10. What is the output of this program?
1. class A {
2. public int i;
3. protected int j;
4. }
5. class B extends A {
6. int j;
7. void display() {
8. super.j = 3;
9. System.out.println(i + " " + j);
10. }
11. }
12. class Output {
13. public static void main(String args[])
14. {
15. B obj = new B();
16. obj.i=1;
17. obj.j=2;
18. obj.display();
19. }
20. }
a) 1 2
b) 2 1
c) 1 3
d) 3 1
View Answer
Answer: a
Explanation: Both class A & B have member with same name that is j, member of class B
will be called by default if no specifier is used. I contains 1 & j contains 2, printing 1 2.
output:
12
a) 0
b) 1
c) 2
d) Compilation Error
View Answer
Answer: c
Explanation: class A & class B both contain display() method, class B inherits class A,
when display() method is called by object of class B, display() method of class B is
executed rather than that of Class A.
output:
2
8. What is the output of this program?
1. final class A {
2. int i;
3. }
4. class B extends A {
5. int j;
6. System.out.println(j + " " + i);
7. }
8. class inheritance {
9. public static void main(String args[])
10. {
11. B obj = new B();
12. obj.display();
13. }
14. }
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
View Answer
Answer: d
Explanation: class A has been declared final hence it cannot be inherited by any other
class. Hence class B does not have member i, giving compilation error.
output:
i cannot be resolved or is not a field
9. What is the output of this program?
1. class A {
2. public int i;
3. private int j;
4. }
5. class B extends A {
6. void display() {
7. super.j = super.i + 1;
8. System.out.println(super.i + " " + super.j);
9. }
10. }
11. class inheritance {
12. public static void main(String args[])
13. {
14. B obj = new B();
15. obj.i=1;
16. obj.j=2;
17. obj.display();
18. }
19. }
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
View Answer
Answer: d
Explanation: class contains a private member variable j, this cannot be inherited by
subclass B and does not have access to it.
output:
The field A.j is not visible
9. What is the output of this program?
1. class A {
2. public void display() {
3. System.out.println("A");
4. }
5. }
6. class B extends A {
7. public void display() {
8. System.out.println("B");
9. }
10. }
11. class Dynamic_dispatch {
12. public static void main(String args[])
13. {
14. A obj1 = new A();
15. B obj2 = new B();
16. A r;
17. r = obj1;
18. r.display();
19. r = obj2;
20. r.display();
21. }
22. }
a) A B
b) B A
c) Runtime Error
d) Compilation Error
View Answer
Answer: a
Explanation: Call to display function of class A and class B is made by using dynamic
method dispatch, by using this method a call to an overridden function is resolved at run
time, rather than at compilation time.
output:
AB
10. What is the output of this program?
1. class A {
2. int i;
3. public void display() {
4. System.out.println(i);
5. }
6. }
7. class B extends A {
8. int j;
9. public void display() {
10. System.out.println(j);
11. }
12. }
13. class Dynamic_dispatch {
14. public static void main(String args[])
15. {
16. B obj2 = new B();
17. obj2.i = 1;
18. obj2.j = 2;
19. A r;
20. r = obj2;
21. r.display();
22. }
23. }
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: r is reference of type A, the program assigns a reference of object obj2 to r
and uses that reference to call function display() of class B.
output:
2
a) 0
b) 2
c) Runtime Error
d) Compilation Error
View Answer
Answer: b
Explanation: class A is an abstract class, it contains a abstract function display(), the full
implementation of display() method is given in its subclass B, Both the display functions
are the same. Prototype of display() is defined in class A and its implementation is given
in class B.
output:
2
8. What is the output of this program?
1. class A {
2. int i;
3. int j;
4. A() {
5. i = 1;
6. j = 2;
7. }
8. }
9. class Output {
10. public static void main(String args[])
11. {
12. A obj1 = new A();
13. A obj2 = new A();
14. System.out.print(obj1.equals(obj2));
15. }
16. }
a) false
b) true
c) 1
d) Compilation Error
View Answer
Answer: a
Explanation: obj1 and obj2 are two different objects. equals() is a method of Object class,
Since Object class is superclass of every class it is available to every object.
output:
false
9. What is the output of this program?
1. class Output {
2. public static void main(String args[])
3. {
4. Object obj = new Object();
5. System.out.print(obj.getclass());
6. }
7. }
a) Object
b) class Object
c) class java.lang.Object
d) Compilation Error
View Answer
Answer: c
Explanation: None.
output:
class java.lang.Object
10. What is the output of this program?
1. class A {
2. int i;
3. int j;
4. A() {
5. i = 1;
6. j = 2;
7. }
8. }
9. class Output {
10. public static void main(String args[])
11. {
12. A obj1 = new A();
13. System.out.print(obj1.toString());
14. }
15. }
a) true
b) false
c) String associated with obj1
d) Compilation Error
View Answer
Answer: c
Explanation: toString() is method of class Object, since it is superclass of every class,
every object has this method. toString() returns the string associated with the calling
object.
output:
A@1cd2e5f
a) Integers
c) Boolean
Answer:c
Explanation: All relational operators return a boolean value i:e true and false.
1.&& 2. == 3. ?: 4. +=
a) 3 & 2
b) 1 & 4
c) 1, 2 & 4
d) 1, 2 & 3
Answer: d
Explanation: Operator Short circuit AND, &&, equal to, == , ternary if-then-else, ?:,
are boolean logical operators. += is an arithmetic operator it can operate only on
numeric values.
a) !
b) |
c) &
d) &&
Answer: d
Explanation: Operator short circuit and, &&, and short circuit or, ||, skip evaluating
right hand operand when output can be determined by left operand alone.
Answer: d
Explanation: true and false are keywords, they are non numeric values which do
no relate to zero or non zero numbers. true and false are boolean values.
a) 1
b) 0
c) true
d) false
View Answer
Answer:d
Explanation: Operator > returns a boolean value. 5 is not greater than 6 therefore false is
returned.
output:
false
7. What is the output of this program?
1. class bool_operator {
2. public static void main(String args[])
3. {
4. boolean a = true;
5. boolean b = !true;
6. boolean c = a | b;
7. boolean d = a & b;
8. boolean e = d ? b : c;
9. System.out.println(d + " " + e);
10. }
11. }
a) false false
b) true ture
c) true false
d) false true
View Answer
Answer: d
Explanation: Operator | returns true if any one operand is true, thus ‘c = true | false’ is
true. Operator & returns a true if both of the operand is true thus d is false. Ternary
operator ?: assigns left of ‘:’ if condition is true and right hand of ‘:’ if condition is false. d
is false thus e = d ? b : c , assigns c to e , e contains true.
output:
false true
8. What is the output of this program?
1. class ternary_operator {
2. public static void main(String args[])
3. {
4. int x = 3;
5. int y = ~ x;
6. int z;
7. z = x > y ? x : y;
8. System.out.print(z);
9. }
10. }
a) 0
b) 1
c) 3
d) -4
View Answer
Answer:c
Explanation: None.
output:
3
9. What is the output of this program?
1. class Output {
2. public static void main(String args[])
3. {
4. int x , y = 1;
5. x = 10;
6. if (x != 10 && x / 0 == 0)
7. System.out.println(y);
8. else
9. System.out.println(++y);
10. }
11. }
a) 1
b) 2
c) Runtime error owing to division by zero in if condition.
d) Unpredictable behavior of program.
View Answer
Answer: b
Explanation: Operator short circuit and, &&, skips evaluating right hand operand if left
hand operand is false thus division by zero in if condition does not give an error.
output:
2
10. What is the output of this program?
1. class Output {
2. public static void main(String args[])
3. {
4. boolean a = true;
5. boolean b = false;
6. boolean c = a ^ b;
7. System.out.println(!c);
8. }
9. }
a) 0
b) 1
c) false
d) true
View Answer
Answer: c
Explanation: None.
output:
false
a) ” Hello World ”
b) ” Hello World ”
c) “Hello World”
d) Hello world
View Answer
Answer: c
Explanation: trim() method is used to remove leading and trailing whitespaces in a string.
Output:
“Hello World”
7. What is the output of this program?
1. class output {
2. public static void main(String args[])
3. {
4. String s1 = "one";
5. String s2 = s1 + " two";
6. System.out.println(s2);
7. }
8. }
a) one
b) two
c) one two
d) compilation error
View Answer
Answer: c
Explanation: None.
Output:
one two
8. What is the output of this program?
1. class output {
2. public static void main(String args[])
3. {
4. String s1 = "Hello";
5. String s2 = s1.replace('l','w');
6. System.out.println(s2);
7. }
8. }
a) hello
b) helwo
c) hewlo
d) hewwo
View Answer
Answer: d
Explanation: replace() method replaces all occurrences of one character in invoking string
with another character. s1.replace(‘l’,’w’) replaces every occurrence of ‘l’ in hello by ‘w’,
giving hewwo.
Output:
hewwo
9. What is the output of this program?
1. class output {
2. public static void main(String args[])
3. {
4. String s1 = "Hello World";
5. String s2 = s1.substring(0 , 4);
6. System.out.println(s2);
7. }
8. }
a) Hell
b) Hello
c) Worl
d) World
View Answer
Answer: a
Explanation: substring(0,4) returns the character from 0 th position to 3 rd position.
output:
Hell
10. What is the output of this program?
1. class output {
2. public static void main(String args[])
3. { String s = "Hello World";
4. int i = s.indexOf('o');
5. int j = s.lastIndexOf('l');
6. System.out.print(i + " " + j);
7.
8. }
9. }
a) 4 8
b) 5 9
c) 4 9
d) 5 8
View Answer
Answer: c
Explanation: indexOf() method returns the index of first occurrence of the character where
as lastIndexOf() returns the index of last occurrence of the character.
output:
49
a) Hello
b) World
c) Helloworld
d) Hello World
Answer: d
Explanation: append() method of class StringBuffer is used to concatenate the string
representation to the end of invoking string.
Output:
$ javac output.java
$ java output
Hello World
8. What is the output of this program?
1. class output {
2. public static void main(String args[])
3. {
4. StringBuffer s1 = new StringBuffer("Hello");
5. s1.setCharAt(1,'x');
6. System.out.println(s1);
7. }
8. }
a) xello
b) xxxxx
c) Hxllo
d) Hexlo
View Answer
Answer: c
Explanation: None.
Output:
$ javac output.java
$ java output
Hxllo
9. What is the output of this program?
1. class output {
2. public static void main(String args[])
3. {
4. StringBuffer s1 = new StringBuffer("Hello World");
5. s1.insert(6 , "Good ");
6. System.out.println(s1);
7. }
8. }
a) HelloGoodWorld
b) HellGoodoWorld
c) HellGood oWorld
d) Hello Good World
View Answer
Answer: d
Explanation: The insert() method inserts one string into another. It is overloaded to accept
values of all simple types, plus String and Objects. Sting is inserted into invoking object
at specified position. “Good ” is inserted in “Hello World” T index 6 giving “Hello Good
World”.
output:
$ javac output.java
$ java output
Hello Good World
10. What is the output of this program?
1. class output {
2. public static void main(String args[])
3. {
4. StringBuffer s1 = new StringBuffer("Hello");
5. s1.deleteCharAt(1);
6. System.out.println(s1);
7. }
8. }
a) Hell
b) ello
c) Hllo
d) Helo
View Answer
Answer: c
Explanation: None.
Output:
$ javac output.java
$ java output
Hllo
a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 4 3 6 9
Answer:a
Explanation: indexof(‘c’) and lastIndexof(‘c’) are pre defined function which are used to
get the index of first and last occurrence of
the character pointed by c in the given array.
Output:
$ javac output.java
$ java output
6469
8. What is the output of this program?
1. class output {
2. public static void main(String args[])
3. {
4. char c[]={'a', '1', 'b' ,' ' ,'A' , '0'};
5. for (int i = 0; i < 5; ++i)
6. {
7. if(Character.isDigit(c[i]))
8. System.out.println(c[i]+" is a digit");
9. if(Character.isWhitespace(c[i]))
10. System.out.println(c[i]+" is a Whitespace
character");
11. if(Character.isUpperCase(c[i]))
12. System.out.println(c[i]+" is an Upper case
Letter");
13. if(Character.isLowerCase(c[i]))
14. System.out.println(c[i]+" is a lower case
Letter");
15. i=i+3;
16. }
17. }
18. }
a) a
b) b
c) c
d) abc
Answer: d
Explanation: String(chars) is a constructor of class string, it initializes string s with the
values stored in character array chars, therefore s contains “abc”.
output:
$ javac String_demo.java
$ java String_demo
abc
10. What is the output of this program?
1. class output {
2. public static void main(String args[])
3. {
4. char ch;
5. ch = "hello".charAt(1);
6. System.out.println(ch);
7. }
8. }
a) h
b) e
c) l
d) o
Answer: b
Explanation: “hello” is a String literal, method charAt() returns the character specified at
the index position. Character at index position 1 is e of hello, hence ch contains e.
output:
$ javac output.java
$ java output
e
a) true
b) false
c) 0
d) 1
Answer: b
Explanation: startsWith() method is case sensitive “hello” and “Hello” are treated
differently, hence false is stored in var.
Output:
$ javac output.java
$ java output
false
7. What is the output of this program?
1. class output {
2. public static void main(String args[])
3. {
4. String s1 = "Hello i love java";
5. String s2 = new String(s1);
6. System.out.println((s1 == s2) + " " + s1.equals(s2));
7. }
8. }
a) true true
b) false false
c) true false
d) false true
Answer: d
Explanation: The == operator compares two object references to see whether they refer to
the same instance, where as equals() compares the content of the two objects.
Output:
$ javac output.java
$ java output
false true
8. What is the output of this program?
1. class output {
2. public static void main(String args[])
3. {
4. String s1 = "Hello";
5. String s2 = new String(s1);
6. String s3 = "HELLO";
7. System.out.println(s1.equals(s2) + " " + s2.equals(s3));
8. }
9. }
a) true true
b) false false
c) true false
d) false true
Answer: c
Explanation: None.
Output:
$ javac output.java
$ java output
true false
9. What is the output of this program?
1. class output {
2. public static void main(String args[])
3. {
4. char ch;
5. ch = "hello".charAt(1);
6. System.out.println(ch);
7. }
8. }
a) h
b) e
c) l
d) o
Answer: b
Explanation: “hello” is a String literal, method charAt() returns the character specified at
the index position. Character at index position 1 is e of hello, hence ch contains e.
output:
e
10. What is the output of this program?
1. class output {
2. public static void main(String args[])
3. {
4. String chars[] = {"a", "b", "c", "a", "c"};
5. for (int i = 0; i < chars.length; ++i)
6. for (int j = i + 1; j < chars.length; ++j)
7. if(chars[i].compareTo(chars[j]) == 0)
8. System.out.print(chars[j]);
9. }
10. }
a) ab
b) bc
c) ca
d) ac
Answer: d
Explanation: compareTo() function returns zero when both the strings are equal, it returns
a value less than zero if the invoking string is less than the other string being compared
and value greater than zero when invoking string is greater than the string compared to.