Java MCQ Set 1
1. Which of these method of class String is used to compare two String objects for their equality?
a) equals()
b) Equals()
c) isequal()
d) Isequal()
Answer
Answer: a [Reason:] None.
2. Which of these methods is used to compare a specific region inside a string with another specific region in another string?
a) regionMatch()
b) match()
c) RegionMatches()
d) regionMatches()
Answer
Answer: d [Reason:] None.
3. Which of these method of class String is used to check weather a given object starts with a particular string literal?
a) startsWith()
b) endsWith()
c) Starts()
d) ends()
Answer
Answer: a [Reason:] Method startsWith() of string class is used to check whether the String in question starts with a specified string. It is specialized form of method regionMatches().
4. What is the value returned by unction compareTo() if the invoking string is less than the string compared?
a) zero
b) value less than zero
c) value greater than zero
d) none of the mentioned
Answer
Answer: b [Reason:] 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.
5. Which of these data type value is returned by equals() method of String class?
a) char
b) int
c) boolean
d) all of the mentioned
Answer
Answer: c [Reason:] equals() method of string class returns boolean value true if both the string are equal and false if they are unequal.
6. What is the output of this program?
-
class output
-
{
-
public static void main(String args[])
-
{
-
String c = "Hello i love java";
-
boolean var;
-
var = c.startsWith("hello");
-
System.out.println(var);
-
}
-
}
a) true
b) false
c) 0
d) 1
Answer
Answer: b [Reason:] 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?
-
class output
-
{
-
public static void main(String args[])
-
{
-
String s1 = "Hello i love java";
-
String s2 = new String(s1);
-
System.out.println((s1 == s2) + " " + s1.equals(s2));
-
}
-
}
a) true true
b) false false
c) true false
d) false true
Answer
Answer: d [Reason:] 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?
-
class output
-
{
-
public static void main(String args[])
-
{
-
String s1 = "Hello";
-
String s2 = new String(s1);
-
String s3 = "HELLO";
-
System.out.println(s1.equals(s2) + " " + s2.equals(s3));
-
}
-
}
a) true true
b) false false
c) true false
d) false true
Answer
Answer: c [Reason:] None.
Output:
$ javac output.java
$ java output
true false
9. In the below code, which code fragment should be inserted at line 3 so that the output will be: “123abc 123abc”?
1 StringBuilder sb1 = new StringBuilder("123"); 2 String s1 = "123"; 3 // insert code here 4 System.out.println(sb1 + " " + s1);
a) sb1.append(“abc”); s1.append(“abc”);
b) sb1.append(“abc”); s1.concat(“abc”);
c) sb1.concat(“abc”); s1.append(“abc”);
d) sb1.append(“abc”); s1 = s1.concat(“abc”);
Answer
Answer: d [Reason:] append() is stringbuffer method and concat is String class method.
append() is stringbuffer method and concat is String class method.
10. What is the output of this program?
-
class output
-
{
-
public static void main(String args[])
-
{
-
String chars[] = {"a", "b", "c", "a", "c"};
-
for (int i = 0; i < chars.length; ++i)
-
for (int j = i + 1; j < chars.length; ++j)
-
if(chars[i].compareTo(chars[j]) == 0)
-
System.out.print(chars[j]);
-
}
-
}
a) ab
b) bc
c) ca
d) ac
Answer
Answer: d [Reason:] 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.
output:
$ javac output.java
$ java output
ac
Java MCQ Set 2
1. Which of these class is superclass of String and StringBuffer class?
a) java.util
b) java.lang
c) ArrayList
d) None of the mentioned
Answer
Answer: b [Reason:] None.
2. Which of these operators can be used to concatenate two or more String objects?
a) +
b) +=
c) &
d) ||
Answer
Answer: a [Reason:] Operator + is used to concatenate strings, Example String s = “i ” + “like ” + “java”; String s contains “I like java”.
3. Which of these method of class String is used to obtain length of String object?
a) get()
b) Sizeof()
c) lengthof()
d) length()
Answer
Answer: d [Reason:] Method length() of string class is used to get the length of the object which invoked method length().
4. Which of these method of class String is used to extract a single character from a String object?
a) CHARAT()
b) chatat()
c) charAt()
d) ChatAt()
Answer
Answer: c [Reason:] None.
5. Which of these constructors is used to create an empty String object?
a) String()
b) String(void)
c) String(0)
d) None of the mentioned
Answer
Answer: a [Reason:] None.
6. Which of these is an oncorrect statement?
a) String objects are immutable, they cannot be changed
b) String object can point to some other reference of String variable
c) StringBuffer class is used to store string in a buffer for later use
d) None of the mentioned
Answer
Answer: c [Reason:] StringBuffer class is used to create strings that can be modified after they are created.
7. What is the output of this program?
-
class String_demo
-
{
-
public static void main(String args[])
-
{
-
char chars[] = {'a', 'b', 'c'};
-
String s = new String(chars);
-
System.out.println(s);
-
}
-
}
a) a
b) b
c) c
d) abc
Answer
Answer: d [Reason:] 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
8. What is the output of this program?
-
class String_demo
-
{
-
public static void main(String args[])
-
{
-
int ascii[] = { 65, 66, 67, 68};
-
String s = new String(ascii, 1, 3);
-
System.out.println(s);
-
}
-
}
a) ABC
b) BCD
c) CDA
d) ABCD
Answer
Answer: b [Reason:] ascii is an array of integers which contains ascii codes of Characters A, B, C, D. String(ascii, 1, 3) is an constructor which initializes s with Characters corresponding to ascii codes stored in array ascii, starting position being given by 1 & ending position by 3, Thus s stores BCD.
output:
$ javac String_demo.java
$ java String_demo
BCD
9. What is the output of this program?
-
class String_demo
-
{
-
public static void main(String args[])
-
{
-
char chars[] = {'a', 'b', 'c'};
-
String s = new String(chars);
-
String s1 = "abcd";
-
int len1 = s1.length();
-
int len2 = s.length();
-
System.out.println(len1 + " " + len2);
-
}
-
}
a) 3 0
b) 0 3
c) 3 4
d) 4 3
Answer
Answer: d [Reason:] None.
output:
$ javac String_demo.java
$ java String_demo
4 3
10. What is the output of this program?
-
class A
-
{
-
int i;
-
int j;
-
A()
-
{
-
i = 1;
-
j = 2;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
A obj1 = new A();
-
System.out.print(obj1.toString());
-
}
-
}
a) True
b) False
c) String associated with obj1
d) Compilation Error
Answer
Answer: c [Reason:] 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:
$ javac Output.java
$ java Output
Java MCQ Set 3
1. Which of these class is used to create an object whose character sequence is mutable?
a) String()
b) StringBuffer()
c) Both of the mentioned
d) None of the mentioned
Answer
Answer: b [Reason:] StringBuffer represents growable and writeable character sequence.
2. Which of these method of class StringBuffer is used to concatenate the string representation to the end of invoking string?
a) concat()
b) append()
c) join()
d) concatenate()
Answer
Answer: b [Reason:] None.
3. Which of these method of class StringBuffer is used to find the length of current character sequence?
a) length()
b) Length()
c) capacity()
d) Capacity()
Answer
Answer: a [Reason:] None.
4. What is the string contained in s after following lines of code?
StringBuffer s new StringBuffer(“Hello”);
s.deleteCharAt(0);
a) Hell
b) ello
c) Hel
d) llo
Answer
Answer: b [Reason:] deleteCharAt() method deletes the character at the specified index location and returns the resulting StringBuffer object.
5. Which of the following statement is correct?
a) reverse() method reverses all characters
b) reverseall() method reverses all characters
c) replace() method replaces first occurrence of a character in invoking string with another character
d) replace() method replaces last occurrence of a character in invoking string with another character
Answer
Answer: a [Reason:] reverse() method reverses all characters. It returns the reversed object on which it was called.
6. What is the output of this program?
-
class output
-
{
-
public static void main(String args[])
-
{
-
String a = "hello i love java";
-
System.out.println(a.indexOf('e')+" "+a.indexOf('a')+" "+a.lastIndexOf('l')+" "+a.lastIndexOf('v'));
-
}
-
}
a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 1 14 8 15
Answer
Answer: a [Reason:] 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
1 14 8 15
7. What is the output of this program?
-
class output
-
{
-
public static void main(String args[])
-
{
-
StringBuffer c = new StringBuffer("Hello");
-
c.delete(0,2);
-
System.out.println(c);
-
}
-
}
a) He
b) Hel
c) lo
d) llo
Answer
Answer: d [Reason:] delete(0,2) is used to delete the characters from 0 th position to 1 st position.
Output:
$ javac output.java
$ java output
llo
8. What is the output of this program?
-
class output
-
{
-
public static void main(String args[])
-
{
-
StringBuffer c = new StringBuffer("Hello");
-
StringBuffer c1 = new StringBuffer(" World");
-
c.append(c1);
-
System.out.println(c);
-
}
-
}
a) Hello
b) World
c) Helloworld
d) Hello World
Answer
Answer: d [Reason:] 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
9. What is the output of this program?
-
class output
-
{
-
public static void main(String args[])
-
{
-
StringBuffer s1 = new StringBuffer("Hello");
-
StringBuffer s2 = s1.reverse();
-
System.out.println(s2);
-
}
-
}
a) Hello
b) olleH
c) HelloolleH
d) olleHHello
Answer
Answer: b [Reason:] reverse() method reverses all characters. It returns the reversed object on which it was called.
Output:
$ javac output.java
$ java output
olleH
10. What is the output of this program?
-
class output
-
{
-
class output
-
{
-
public static void main(String args[])
-
{
-
char c[]={'A', '1', 'b' ,' ' ,'a' , '0'};
-
for (int i = 0; i < 5; ++i)
-
{
-
i++;
-
if(Character.isDigit(c[i]))
-
System.out.println(c[i]+" is a digit");
-
if(Character.isWhitespace(c[i]))
-
System.out.println(c[i]+" is a Whitespace character");
-
if(Character.isUpperCase(c[i]))
-
System.out.println(c[i]+" is an Upper case Letter");
-
if(Character.isLowerCase(c[i]))
-
System.out.println(c[i]+" is a lower case Letter");
-
i++;
-
}
-
}
-
}
a) a is a lower case Letter
is White space character
b) b is a lower case Letter
is White space character
c) 1 is a digit
a is a lower case Letter
d) a is a lower case Letter
0 is a digit
Answer
Answer: c [Reason:] Character.isDigit(c[i]),Character.isUpperCase(c[i]),Character.isWhitespace(c[i]) are the function of library java.lang they are used to find whether the given character is of specified type or not. They return true or false i:e Boolean variable.
Output:
$ javac output.java
$ java output
1 is a digit
a is a lower case Letter
Java MCQ Set 4
1. Which of these method of class StringBuffer is used to extract a substring from a String object?
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned
Answer
Answer: a [Reason:] None.
2. What will s2 contain after following lines of code?
StringBuffer s1 = "one"; StringBuffer s2 = s1.append("two")
a) one
b) two
c) onetwo
d) twoone
Answer
Answer: c [Reason:] Two strings can be concatenated by using append() method.
3. Which of these method of class StringBuffer is used to reverse sequence of characters?
a) reverse()
b) reverseall()
c) Reverse()
d) reverseAll()
Answer
Answer: a [Reason:] reverse() method reverses all characters. It returns the reversed object on which it was called.
4. Which of these method of class StringBuffer is used to get the length of sequence of characters?
a) length()
b) capacity()
c) Length()
d) Capacity()
Answer
Answer: a [Reason:] length()- returns the length of String the StringBuffer would create whereas capacity() returns total number of characters that can be supported before it is grown.
5. Which of the following are incorrect form of StringBuffer class constructor?
a) StringBuffer()
b) StringBuffer(int size)
c) StringBuffer(String str)
d) StringBuffer(int size , String str)
Answer
Answer: d [Reason:] None.
6. What is the output of this program?
-
class output
-
{
-
public static void main(String args[])
-
{
-
StringBuffer c = new StringBuffer("Hello");
-
System.out.println(c.length());
-
}
-
}
a) 4
b) 5
c) 6
d) 7
Answer
Answer: b [Reason:] length() method is used to obtain length of StringBuffer object, length of “Hello” is 5.
Output:
$ javac output.java
$ java output
5
7. What is the output of this program?
-
class output
-
{
-
public static void main(String args[])
-
{
-
StringBuffer sb=new StringBuffer("Hello");
-
sb.replace(1,3,"Java");
-
System.out.println(sb);
-
}
-
}
a) Hello java
b) Hellojava
c) HJavalo
d) Hjava
Answer
Answer: c [Reason:] The replace() method replaces the given string from the specified beginIndex and endIndex.
$ javac output.java
$ java output
HJavalo
8. What is the output of this program?
-
class output
-
{
-
public static void main(String args[])
-
{
-
StringBuffer s1 = new StringBuffer("Hello");
-
s1.setCharAt(1,'x');
-
System.out.println(s1);
-
}
-
}
a) xello
b) xxxxx
c) Hxllo
d) Hexlo
Answer
Answer: c [Reason:] None.
Output:
$ javac output.java
$ java output
Hxllo
9. What is the output of this program?
-
class output
-
{
-
public static void main(String args[])
-
{
-
StringBuffer s1 = new StringBuffer("Hello World");
-
s1.insert(6 , "Good ");
-
System.out.println(s1);
-
}
-
}
a) HelloGoodWorld
b) HellGoodoWorld
c) HellGood oWorld
d) Hello Good World
Answer
Answer: d [Reason:] 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?
-
class output
-
{
-
public static void main(String args[])
-
{
-
StringBuffer s1 = new StringBuffer("Hello");
-
s1.insert(1,"Java");
-
System.out.println(s1);
-
}
-
}
a) hello
b) java
c) Hello Java
d) HelloJava
Answer
Answer: d [Reason:] Insert method will insert string at a specified position
Output:
$ javac output.java
$ java output
HelloJava
Java MCQ Set 5
1. Which of these access specifiers must be used for main() method?
a) private
b) public
c) protected
d) none of the mentioned
Answer
Answer: b [Reason:] main() method must be specified public as it called by Java run time system, outside of the program. If no access specifier is used then by default member is public within its own package & cannot be accessed by Java run time system.
2. Which of these is used to access member of class before object of that class is created?
a) public
b) private
c) static
d) protected
Answer
Answer: c [Reason:] None.
3. Which of these is used as default for a member of a class if no access specifier is used for it?
a) private
b) public
c) public, within its own package
d) protected
Answer
Answer: a [Reason:] When we pass an argument by call-by-value a copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have no effect on original argument, they remain the same.
4. What is the process by which we can control what parts of a program can access the members of a class?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
Answer
Answer: c [Reason:] None.
5. Which of the following statements are incorrect?
a) public members of class can be accessed by any code in the program
b) private members of class can only be accessed by other members of the class
c) private members of class can be inherited by a sub class, and become protected members in sub class
d) protected members of a class can be inherited by a sub class, and become private members of the sub class
Answer
Answer: c [Reason:] private members of a class can not be inherited by a sub class.
6. What is the output of this program?
-
class access
-
{
-
public int x;
-
private int y;
-
void cal(int a, int b)
-
{
-
x = a + 1;
-
y = b;
-
}
-
}
-
class access_specifier
-
{
-
public static void main(String args[])
-
{
-
access obj = new access();
-
obj.cal(2, 3);
-
System.out.println(obj.x + " " + obj.y);
-
}
-
}
a) 3 3
b) 2 3
c) Runtime Error
d) Compilation Error
Answer
Answer: c [Reason:] None.
output:
$ javac access_specifier.java
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The field access.y is not visible
7. What is the output of this program?
-
class access
-
{
-
public int x;
-
private int y;
-
void cal(int a, int b)
-
{
-
x = a + 1;
-
y = b;
-
}
-
void print()
-
{
-
system.out.println(" " + y);
-
}
-
}
-
class access_specifier
-
{
-
public static void main(String args[])
-
{
-
access obj = new access();
-
obj.cal(2, 3);
-
System.out.println(obj.x);
-
obj.print();
-
}
-
}
a) 2 3
b) 3 3
c) Runtime Error
d) Compilation Error
Answer
Answer: b [Reason:] None.
output:
$ javac access_specifier.java
$ java access_specifier
3 3
8. What is the output of this program?
-
class static_out
-
{
-
static int x;
-
static int y;
-
void add(int a, int b)
-
{
-
x = a + b;
-
y = x + b;
-
}
-
}
-
class static_use
-
{
-
public static void main(String args[])
-
{
-
static_out obj1 = new static_out();
-
static_out obj2 = new static_out();
-
int a = 2;
-
obj1.add(a, a + 1);
-
obj2.add(5, a);
-
System.out.println(obj1.x + " " + obj2.y);
-
}
-
}
a) 7 7
b) 6 6
c) 7 9
d) 9 7
Answer
Answer: c [Reason:] None.
output:
$ javac static_use.java
$ java static_use
6 6.4
9. Which of these access specifier must be used for class so that it can be inherited by another sub class?
a) public
b) private
c) protected
d) none of the mentioned
Answer
Answer: a [Reason:] None.
10. What is the output of this program?
-
class test
-
{
-
int a;
-
int b;
-
test(int i, int j)
-
{
-
a = i;
-
b = j;
-
}
-
void meth(test o)
-
{
-
o.a *= 2;
-
O.b /= 2;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
test obj = new test(10 , 20);
-
obj.meth(obj);
-
System.out.println(obj.a + " " + obj.b);
-
}
-
}
a) 10 20
b) 20 10
c) 20 40
d) 40 20
Answer
Answer: b [Reason:] Class objects are always passed by reference, therefore changes done are reflected back on original arguments. obj.meth(obj) sends object obj as parameter whose variables a & b are multiplied and divided by 2 respectively by meth() function of class test. a & b becomes 20 & 10 respectively.
output:
$ javac Output.java
$ java Output
20 10