Java Part 4: Questions and Answers on Inheritance and String Handling
d
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)
a
Which of the following statement is correct? a) replace() method replaces all occurrences of one character in invoking string with another character. b) replace() method replaces only first occurances of a character in invoking string with another character. c) replace() method replaces all the characters in invoking string with another character. d) replace() replace() method replaces last occurrence of a character in invoking string with another character.
c
Which of these methods can be used to convert all characters in a String into a character array? a) charAt() b) both getChars() & charAt() c) both toCharArray() & getChars() d) All of the mentioned
a
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.
b
Which of these class is superclass of String and StringBuffer class? a) java.util b) java.lang c) ArrayList d) None of the mentioned
b
Which of these class is superclass of every class in Java? a) String class b) Object class c) Abstract class d) ArrayList class
b
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
b
Which of these class relies upon its subclasses for complete implementation of its methods? a) Object class b) abstract class c) ArrayList class d) None of the mentioned
a
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
c
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
c
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
d
Which of these is correct way of calling a constructor having no parameters, of superclass A by subclass B? a) super(void); b) superclass.(); c) super.A(); d) super();
c
Which of these is correct way of inheriting class A by class B? a) class B + class A {} b) class B inherits class A {} c) class B extends A {} d) class B extends class A {}
c
Which of these is supported by method overriding in Java? a) Abstraction b) Encapsulation c) Polymorphism d) None of the mentioned
a
Which of these keyword can be used in subclass to call the constructor of superclass? a) super b) this c) extent d) extends
d
Which of these keyword must be used to inherit a class? a) super b) this c) extent d) extends
d
Which of these keywords can be used to prevent Method overriding? a) static b) constant c) protected d) final
d
Which of these keywords can be used to prevent inheritance of a class? a) super b) constant c) Class d) final
a
Which of these keywords cannot be used for a class which has been declared final? a) abstract b) extends c) abstract and extends d) None of the mentioned
b
Which of these keywords is used to refer to member of base class from a sub class? a) upper b) super c) this d) None of the mentioned
c
Which of these method of Object class can clone an object? a) Objectcopy() b) copy() c) Object clone() d) clone()
c
Which of these method of Object class is used to obtain class of an object at run time? a) get() b) void getclass() c) Class getclass() d) None of the mentioned
a
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()
a
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()
c
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()
a
Which of these method of class String is used to extract a substring from a String object? a) substring() b) Substring() c) SubString() d) None of the mentioned
d
Which of these method of class String is used to extract more than one character at a time a String object? a) getchars() b) GetChars() c) Getchars() d) getChars()
d
Which of these method of class String is used to obtain length of String object? a) get() b) Sizeof() c) lengthof() d) length()
b
Which of these method of class String is used to remove leading and trailing whitespaces? a) startsWith() b) trim() c) Trim() d) doTrim()
b
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()
a
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
a
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()
a
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()
a
Which of these method of class StringBuffer is used to reverse sequence of characters? a) reverse() b) reverseall() c) Reverse() d) reverseAll()
a
Which of these methods is an alternative to getChars() that stores the characters in an array of bytes? a) getBytes() b) GetByte() c) giveByte() d) Give Bytes()
d
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()
a
Which of these operators can be used to concatenate two or more String objects? a) + b) += c) & d) ||
a
Which two classes use the Shape class correctly? (Choose two.) A. public class Circle implements Shape { private int radius; } B. public abstract class Circle extends Shape { private int radius; } C. public class Circle extends Shape { private int radius; public void draw(); } D. public abstract class Circle implements Shape { private int radius; public void draw(); } E. public class Circle extends Shape { private int radius; public void draw() { /* code here */ } } F. public abstract class Circle implements Shape { private int radius; public void draw() { /* code here */ } } a) B,E b) A,C c) C,E d) T,H
d
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
b
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
d
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
b
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
d
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
c
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
c
What is the output of this program? class output { public static void main(String args[]) { String s = "Hello World"; int i = s.indexOf('o'); int j = s.lastIndexOf('l'); System.out.print(i + " " + j); } } a) 4 8 b) 5 9 c) 4 9 d) 5 8
c
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
d
What is the output of this program? final class A { int i; } class B extends A { int j; System.out.println(j + " " + i); } class inheritance { public static void main(String args[]) { B obj = new B(); obj.display(); } } a) 2 2 b) 3 3 c) Runtime Error d) Compilation Error
b
What is the process of defining a method in 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
b
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
c
What is the value returned by function compareTo() if the invoking string is greater than the string compared? a) zero b) value less than zero c) value greater than zero d) None of the mentioned
b
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
d
What will be output of the following code? public class Boxer1 { Integer i; int x; public Boxer1(int y) { x = i+y; System.out.println(x); } public static void main(String[] args) { new Boxer1 (new Integer(4)); } } a) The value "4" is printed at the command line b) Compilation fails because of an error in line c) A NullPointerException occurs at runtime d) An IllegalStateException occurs at runtime
c
What will s2 contain after following lines of code? String s1 = "one"; String s2 = s1.concat("two") a) one b) two c) onetwo d) twoone
c
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
b
A class member declared protected becomes member of subclass of which type? a) public member b) private member c) protected member d) static member
d
At line number 2 below, choose 3 valid data-type attributes/qualifiers among "final, static, native, public, private, abstract, protected" public interface Status { /* insert qualifier here */ int MY_VALUE = 10; } a) final, native, private b) final, static, protected c) final, private, abstract d) final, static, public
c
What is the output of this program? class Alligator { public static void main(String[] args) { int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}}; int [][]y = x; System.out.println(y[2][1]); } } a) 2 b) 3 c) 7 d) Compilation Error
c
What is the output of this program? class Output { public static void main(String args[]) { Object obj = new Object(); System.out.print(obj.getclass()); } } a) Object b) class Object c) class java.lang.Object d) Compilation Error
d
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
c
In below code, what can directly access and change the value of the variable name? package test; class Target { public String name = "hello"; } a) any class b) only the Target class c) any class in the test package d) any class that extends Target
d
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");
d
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
b
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
d
What is the output of this program? class output { public static void main(String args[]) { String s1 = "Hello"; String s2 = s1.replace('l','w'); System.out.println(s2); } } a) hello b) helwo c) hewlo d) hewwo
c
What is the output of this program? class output { public static void main(String args[]) { String s1 = "one"; String s2 = s1 + " two"; System.out.println(s2); } } a) one b) two c) one two d) compilation error
b
What is the output of this program? abstract class A { int i; abstract void display(); } class B extends A { int j; void display() { System.out.println(j); } } class Abstract_demo { public static void main(String args[]) { B obj = new B(); obj.j=2; obj.display(); } } a) 0 b) 2 c) Runtime Error d) Compilation Error
a
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(); A obj2 = new A(); System.out.print(obj1.equals(obj2)); } } a) false b) true c) 1 d) Compilation Error
c
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
b
What is the output of this program? class A { int i; public void display() { System.out.println(i); } } class B extends A { int j; public void display() { System.out.println(j); } } class Dynamic_dispatch { public static void main(String args[]) { B obj2 = new B(); obj2.i = 1; obj2.j = 2; A r; r = obj2; r.display(); } } a) 1 b) 2 c) 3 d) 4
c
What is the output of this program? class A { int i; void display() { System.out.println(i); } } class B extends A { int j; void display() { System.out.println(j); } } class inheritance_demo { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } a) 0 b) 1 c) 2 d) Compilation Error
c
What is the output of this program? class A { int i; } class B extends A { int j; void display() { super.i = j + 1; System.out.println(j + " " + i); } } class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } a) 2 2 b) 3 3 c) 2 3 d) 3 2
d
What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } a) 2 2 b) 3 3 c) Runtime Error d) Compilation Error
a
What is the output of this program? class A { public int i; protected int j; } class B extends A { int j; void display() { super.j = 3; System.out.println(i + " " + j); } } class Output { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } a) 1 2 b) 2 1 c) 1 3 d) 3 1
a
What is the output of this program? class A { public int i; public int j; A() { i = 1; j = 2; } } class B extends A { int a; B() { super(); } } class super_use { public static void main(String args[]) { B obj = new B(); System.out.println(obj.i + " " + obj.j) } } a) 1 2 b) 2 1 c) Runtime Error d) Compilation Error
d
What is the output of this program? class Abc { public static void main(String[]args) { String[] elements = { "for", "tea", "too" }; String first = (elements.length > 0) ? elements[0]: null; } } a) Compilation error b) An exception is thrown at run time c) The variable first is set to null d) The variable first is set to elements[0].
c
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
d
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
a
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('i')+" "+a.indexOf('o') +" "+a.lastIndexOf('i')+" "+a.lastIndexOf('o')); } } a) 6 4 6 9 b) 5 4 5 9 c) 7 8 8 9 d) 4 3 6 9
d
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
a
What is the output of this program? class output { public static void main(String args[]) { String s1 = "Hello World"; String s2 = s1.substring(0 , 4); System.out.println(s2); } } a) Hell b) Hello c) Worl d) World
d
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
c
What is the output of this program? class output { public static void main(String args[]) { char c[]={'a', '1', 'b' ,' ' ,'A' , '0'}; for (int i = 0; i < 5; ++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=i+3; } } } a) a is a lower case Letter is White space character b) b is a lower case Letter is White space character c) a is a lower case Letter A is a upper case Letter d) a is a lower case Letter 0 is a digit
b
What is the output of this program? class output { public static void main(String args[]) { char ch; ch = "hello".charAt(1); System.out.println(ch); } } a) h b) e c) l d) o
c
What is the output of this program? class output { public static void main(String args[]) { String c = " Hello World "; String s = c.trim(); System.out.println("\""+s+"\""); } } a) ""Hello World"" b) ""Hello World" c) "Hello World" d) Hello world
b
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
d
What is the output of this program? class output { public static void main(String args[]) { String c = "Hello i love java"; int start = 2; int end = 9; char s[]=new char[end-start]; c.getChars(start,end,s,0); System.out.println(s); } } a) Hello, i love java b) i love ja c) lo i lo d) llo i l
d
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
c
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
