Professional Documents
Culture Documents
7. How would you declare and initialize the array to declare an array of fruits?
ANS: String[] arrayOfFruits = {"apple", "mango", "orange"};
String concepts:
1. What function does the trim() method of the String class perform?
ANS: It returns a string where both the leading and trailing white space of the
original string has been removed.
2. Which one of the following operators cannot be used in conjunction with a String
object?
ANS:
4. Which statement concerning the charAt() method of the String class is true?
ANS:
5. Which one of the expressions will evaluate to true if preceded by the following
code?
ANS:
6. String a = "hello";
String c = a;
8. Which expression will extract the substring "kap" from a string defined by String
str = "kakapo"?
ANS: str.substring(2, 5)
String s1 = "abc";
String s2 = "def";
String s3 = s2;
s2 = "ghi";
System.out.println(s1 + s2 + s3);
}
What is the result?
ANS:* abcghidef
y = "abc";
x = x + y;
How many String objects have been created? Assume the code given above is a
portion of the code present in a method.
ANS:3
char b = str.charAt(2);
str = str + b;
System.out.println(str);
What is the result? Assume the code given above is a portion of the code present
in a method.
ANS:* atii
13. What will be the result of attempting to compile and run the following code?
}
ANS:* error
String x = "xyz";
x.toUpperCase();
y = y + "abc";
System.out.println(y);
What is the result? Assume the code given above is a portion of the code present
in a method.
ANS:* error
String s = "Contentment!";
System.out.println(s.lastIndexOf(nt, middle));
}
ANS: 5
17. What will be written to the standard output when the following program is run?
composite.concat("server");
System.out.println(trimmed.length());
}
ANS: 7
18. Which expression will evaluate to true?
ANS: "Hello there".toLowerCase().equals("hello there")
String d = "bookkeeper";
d.substring(1,7);
d = "w" + d;
d.append("woo")
System.out.println(D. ;
What is the result? Assume, the code given above is a portion of the code present
in a method.
ANS: Compilation fails.
7. If x, y, and z are all integers, which expression will produce a runtime error?
NOTE: The expressions are always evaluated with all the integers having a value
of 1.
ANS: z = y%--x
8. Given a variable x of type int (which contains a positive value), which is the
correct way of doubling the value of x, barring any wrapping of out-of-range
intermediate values?
ANS: x << 1;
Which expression sets the value of z to x if result has a value of 1, and the value
of y to x otherwise?
ANS: x = (result == 1) ? z : y;
10. Given:
int x = 7;
x <<= 2;
What best describes the second line of code?
ANS: It assigns the value to x after shifting x to 2 places left.
int one = 1;
int two = 2;
char initial = '2';
boolean flag = true;
Which one of the following is invalid?
ANS: switch( flag ){}
12. Identify the shift operator that returns -1 as the value of the variable in the
following statement:
ANS:
1. Which range of values is valid for all integral types, where n is the number of
bits?
ANS:* 0 to 2^(n-1)
10. In which of the following answers does the number of bits increase from
fewest (on the left) to most (on the right)?
ANS: byte short int long
13. Given:
int a = 4;
byte b = 0;
15. Given the following code within a method, which statement is true?
int a, b;
b=5;
ANS: Local variable a is declared but not initialized.
16. Given:
int index = 2;
8. Can an abstract class define both abstract methods and non-abstract methods?
ANS: Yes-the child classes inherit both.
12. Which declaration in the below code represents a valid declaration within the
interface?
1. public interface TestInterface {
2. volatile long value=98L;
3. transient long amount=67L;
4. Long calculate(long input);
5. static Integer getValue();
6. }
ANS: Declaration at line 4
13. Given:
1. public interface Alpha {
2. String MESSAGE = "Welcome";
3. public void display();
4. }
To create an interface called Beta that has Alpha as its parent, which interface
declaration is correct?
ANS: public interface Beta extends Alpha { }
14. interface I1 {
void draw();
}
class C implements I1 {
xxxxxx
}
Which of the following when inserted at xxxxxx is a legal definition and
implementation?
ANS: public void draw() { }
17. If a class inheriting an abstract class does not define all of its methods, then it will be
known as
ANS: Abstract class.
Inheritance Concepts
1. Which statement is true?
ANS: It is possible for a subclass to define a method with the same name and
parameters as a method defined by the superclass.
2. A class Car and its subclass Yugo both have a method run() which was written by the
programmer as part of the class definition. If junker refers to an object of type Yugo,
what will the following code do?
junker.run();
ANS: The run() method defined in Yugo will be called.
3. Here is a situation:
Birthday happy;
happy = new AdultBirthday( "Joe", 39);
happy.greeting();
Which greeting() method is run ?
ANS: The one defined for AdultBirthday because that is the type of the object referred to
by happy.
4. Can an object of a child type be assigned to a variable of the parent type? For
example,
Card crd;
BirthDay bd = new BirthDay("Lucinda", 42);
crd = bd; // is this correct?
ANS: Yes-an object can be assigned to a reference variable of the parent type.
5. class A { A(int i) {} } // 1
class B extends A { } // 2
Which one of the following statements is correct?
ANS: Compile-time error at 2.
6. You want subclasses in any package to have access members of a superclass. Which
is the most restrictive access modifier that will accomplish this objective?
ANS: Protected
16. Which of this keyword can be used in a subclass to call the constructor of superclass?
ANS: super
Packages
1. Which is true about the import statement in Java?
ANS: The import statement must be the first statement after any package
declaration in a file.
2. You decide that you wish to add your application’s class to a group of classes that
are stored in the location /examples/basics.
Complete the code to do this
ANS: package examples.basics;
3. Suppose you are creating a class named Button that you want to include in a group
of related classes called controls.
Identify the correct code that includes the class in that group.
ANS:package controls
5. Suppose a class named App1 is located in the samples messages package. You
have compiled the class. How do you execute the class?
ANS: java samples.messages.App1
Ans: class
3. Which of the following is a valid declaration of an object of class Box?
Ans: new
5. What is the output of this program?
class box
{
int width;
int height;
int length;
}
class mainclass
{
public static void main(String args[])
{
box obj = new box();
obj.width = 10;
obj.height = 2;
obj.length = 10;
int y = obj.width * obj.height * obj.length;
System.out.print(y);
}
}
Ans: 200
Collections
1. Which one of the method below is defined by Map Interface?
Ans: remove
2. Which among the following allow duplicate elements?
Ans: ArrayList
3. Which among the following have elements in insertion order?
Ans: LinkedHashMap
4. How does the set collection deal with duplicate elements?
Ans: The add method returns false if you attempt to add an element with a duplicate value
5. Which of the following statement is true?
Ans: All methods defined in Set interface are also defined in Collection interface
7. Which implementation of the List interface provides for the fastest insertion of a new
element into the middle of the list?
Ans: LinkedList
8. A vector is created and three strings called String1, String2 and String3 are added to
it. The method removeElement("String2") is called. Which of the following methods
will retrieve the "String3".
Ans: get(1)
14.Given:
import java.util.Map;
import java.util.LinkedHashMap;
public class MyMap {
public static void main(String args[]) {
Map m = new LinkedHashMap();
m.put("Fruit", "Apple");
m.put("Vegetable", "Brinjal");
m.put("Snacks", "Burger");
System.out.println(m);
}
}
What is the output?
Ans: Prints {Fruit=Apple, Vegetable=Brinjal, Snacks=Burger}
15. Given:
import java.util.*;
public class TestSortedMap {
public static void main(String args[]) {
Object o = new LinkedHashMap();
System.out.print((o instanceof Map) + ",");
System.out.println(o instanceof SortedMap);
}
}
What is the result of attempting to compile and run the program?
Ans: true, false
16. What will happen when the following code is executed?
import java.util.*;
public class Test3 {
public static void main(String a[]) {
Map s = new HashMap();
s.put("1", "one");
s.put("3", "three");
s.put("2", "two");
System.out.println(s);
}
}
Ans: Cannot predict the output order in which the key values are printed
Constructor
1. What is true about private constructor?
Ans: Private constructor ensures only one instance of a class exist at any point of time
2. What is true about constructor?
Method Overloading
1. What is process of defining two or more methods within same class that have same name
but different parameters declaration?
Class method :
1. Here is a method definition:
int compute( int a, double y ){ . . . .}
Which of the following has a different signature?
Ans: int compute( int a, int y ){ . . . .}
2.Which one of the below statements is true?
Ans: When a class has defined constructors with parameters, the compiler does not create a
default no-args constructor.
3. What is a method's signature?
Ans: The signature of a method is the name of the method and the data types of its
parameters.
4. public class MethodTest {
public void methodSam( int a, float b, byte c) {}
}
Which of the following is considered as overloaded methodSam?
Ans: public void methodSam( int x, float y, byte z) {}
5.A constructor is used to:
Ans: Initialize a newly created object.
6. public class Constructor {
public Constructor (int x, int y, int z)
{}
}
Which of the following is considered as overloaded constructor?
Ans: Constructor() {}
7. Given the following,
1. long test( int x, float y) {
2.
3. }
Which one of the following line inserted at line 2 would not compile?
Ans: return (y/x);
8. Which one of the following is generally a valid definition of an application’s main()
method ?
Ans: public static void main( String[] args );
Methods:
1. What is the return type of a method that does not returns any value?
Ans: void
2. Which method can be defined only once in a program?
Ans: main method
3. Which of these statement is incorrect?
Ans: All object of a class are allotted memory for the all the variables defined in the class
4. What is the output of this program?
class box
{
int width;
int height;
int length;
int volume;
void volume(int height, int length, int width)
{
volume = width*height*length;
}
}
class Prameterized_method
{
public static void main(String args[])
{
box obj = new box();
obj.height = 1;
obj.length = 5;
obj.width = 5;
obj.volume(3,2,1);
System.out.println(obj.volume);
}
}
Ans: 6
5. What is the output of this program?
class equality
{
int x;
int y;
boolean isequal()
{
return(x == y);
}
}
class Output
{
public static void main(String args[])
{
equality obj = new equality();
obj.x = 5;
obj.y = 5;
System.out.println(obj.isequal());
}
}
Ans: true
6. What is the output of this program?
class box
{
int width;
int height;
int length;
int volume;
void volume()
{
volume = width*height*length;
}
}
class Output
{
public static void main(String args[])
{
box obj = new box();
obj.height = 1;
obj.length = 5;
obj.width = 5;
obj.volume();
System.out.println(obj.volume);
}
}
Ans: 25
7. In the below code, which call to sum() method is appropriate?
class Output
{
public static int sum(int ...x)
{
return;
}
static void main(String args[])
{
sum(10);
sum(10,20);
sum(10,20,30);
sum(10,20,30,40);
}
}
Ans: all of the mentioned
8. What is the output of this program?
class area
{
int width;
int length;
int volume;
area()
{
width=5;
length=6;
}
void volume()
{
volume = width*length*height;
}
}
class cons_method
{
public static void main(String args[])
{
area obj = new area();
obj.volume();
System.out.println(obj.volume);
}
}
Ans: error
9. What is the output of the following program?
class Main {
public static void main(String args[]) {
System.out.println(fun());
}
int fun() {
return 20;
}
}
Ans: Compiler error