Professional Documents
Culture Documents
Set B
Set B
Set B
//Basic Variables---Level1
class main_class {
public static void main(String args[])
{
int x = 9;
if (x == 9) {
int x = 8;
System.out.println(x);
}
}
}
A. 9
B. 8
C. Compilation Error
D. Runtime Error
---> Correct Answer : OPTION C, Compilation Error. Two variables with the same name can't be
created in a class.
A. Default constructor is called at the time of declaration of the object if a constructor has not been
defined.
C. finalize() method is called when a object goes out of scope and is no longer needed.
Correct Answer : OPTION C ,finalize() method is called when a object goes out of scope and is no
longer needed.
Q 4). What is the output of this program?//Basic static----Level1
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
Correct Answer : OPTION D, Compilation Error. This is because variable y has private access modifier.
D. All arrays contain an attribute-length which contains the number of elements stored in the array.
A. &
B. &=
C. |=
D. <=
class bitwise_operator {
public static void main(String args[])
{
int var1 = 42;
int var2 = ~var1;
System.out.print(var1 + " " + var2);
}
}
A. 42 42
B. 43 43
C. 42 -43
D. 42 -42
Correct Answer : OPTION C, 42 -43. Unary not operator, ~, inverts all of the bits of its operand.
assigning it to var2 we get inverted value of 42 i:e 11010101 which is -43 in decimal.
class leftshift_operator {
public static void main(String args[])
{
byte x = 64;
int i;
byte y;
i = x << 2;
y = (byte) (x << 2);
System.out.print(i + " " + y);
}
}
A. 64 0
B. 0 64
C. 0 256
D. 256 0
Q 9). How do you ensure that N thread can access N resources without deadlock? //Thread--- Level2
D. Not Possible
Correct Answer : OPTION B, By acquiring resources in a particular order and release resources in
same order.
class comma_operator {
public static void main(String args[])
{
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum);
}
}
A. 5
B. 6
C. 14
D. compilation error
Correct Answer : OPTION B, 6. Using comma operator , we can include more than one statement in
the initialization and
iteration portion of the for loop. Therefore both ++i and j = i + 1 is executed.
Q 11). Can you pass List<String> to a method which accepts List<Object>? //Collection -- Level2
A. Yes
At fist glance it looks like String is object so List<String> can be used where List<Object> is required
It does make sense if you go one step further because List<Object> can store anything including
String,
class Output {
public static void main(String args[])
{
int x, y = 1;
x = 10;
if (x != 10 && x / 0 == 0)
System.out.println(y);
else
System.out.println(++y);
}
}
A. 1
B. 2
D. Unpredictable behaviour
Correct Answer : OPTION B, 2. 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.
Q 13). Which collection class associates values with keys, and orders the keys according to their
natural order?// Collection---Level1
A. java.util.HashSet
B. java.util.LinkedList
C. java.util.TreeMap
D. java.util.SortedSet
A. True
B. False
https://www.careerride.com/question-20-Java-Part-2
A. True
B. False
class average {
public static void main(String args[])
{
double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
double result;
result = 0;
for (int i = 0; i < 6; ++i)
result = result + num[i];
System.out.print(result/6);
}
}
A. 16.34
B. 16.5555
C. 16.46666666666667
D. 16.4666666666
Correct Answer : OPTION C, 16.46666666666667. Simple Average is performed over all the numbers.
Q 17). What is the output of this program?// Casting---Level3
class conversion {
public static void main(String args[])
{
double a = 295.04;
int b = 300;
byte c = (byte) a;
byte d = (byte) b;
System.out.println(c + " " + d);
}
}
A. 38 43
B. 39 44
C. 295 300
D. 295.4 300.6
Correct Answer : OPTION B, 39 44. 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.
class array_output {
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = i;
System.out.print(array_variable[i] + " ");
i++;
}
}
}
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
Correct Answer : OPTION A, 0 2 4 6 8. 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
A. executeQuery()
B. executeUpdate()
C. getConnection()
D. prepareCall()
Q 20). All raw data types should be read and uploaded to the database as an array of?// Basic--Type1
A. int
B. char
C. boolean
D. byte
A. java.util.Time
B. java.sql.Time
C. java.util.Date
Q22) . Given below is a pseudocode that uses a stack. What will be the output for input Studytonight
for the given pseudocode:// / DS---Level1
A. StudytonightStudytonight
B. thginotydutS
C. Studytonight
D. thginotydutSthginotydutS
Correct Answer : OPTION B, thginotydutS. Since the stack data structure follows LIFO order.
When we pop() items from stack, they are popped in reverse order of their insertion (or push())
Correct Answer : OPTION A, One stack is enough. Any expression can be converted into Postfix or
Prefix form.
Q 24). The result of evaluating the postfix expression 10 5 + 60 6 / * 8 - will be?// DS ---Level4
A. 284
B. 213
C. 142
D. 71
What would be the effect on the time complexity of the push and pop operations of the stack
implemented using linked list
(Assuming stack is implemented efficiently)?// DS---Level3
Correct Answer : OPTION B, Stack can be implemented using link list having O(1)
bounds for both insertion as well as deletion by inserting and deleting the element from the
beginning of the list.
System.out.println(str.equals(str1));
System.out.println(str == str1);
System.out.println( str.compareTo(str1) );
B. true true 0
D. true false 0
A. STUDYTONIGHT
B. STUDYtoday.COM
C. STUDYTONIGHT.COM
D. STUDYtoday
A. by James Ghosling
B. y James Ghosling
C. James Ghosling
Q 29). What will be the output of the program ?// Variables--- Level3
class A
{
int x = 10;
public void assign(int x)
{
x = x;
System.out.println(this.x);
}
public static void main(String[] args)
{
new A().assign(100);
}
}
A. 10
B. 100
C. 0
D. compile-time error
Correct Answer : OPTION A, In the statement x = x; x is a local variable declared inside parameter of
assign() method.
And as you know local variable will always get preference over instance variable. So local variable x
will assign the value 100 to itself.
And when we print this.x, it will print the value of instance variable x which is 10.
A. 100
B. 101
C. Error in line 13
D. 0
Correct Answer : OPTION B, 101. Static variable has only one single storage.
All the objects of the class having static variable will have the same instance of static variable.
Q 31). Given the following code, which line will generate an error ?//Static ---Level1
class Test
{
static int x = 100; // line 3
int y = 200; // line 4
public static void main(String []args)
{
final int z; // line 7
z = x + y; // line 8
System.out.println(z);
}
}
A. line 3
B. line 4
C. line 7
D. line 8
Correct Answer : OPTION D, line 8, because non-static variable y cannot be referenced from a static
context i.e main method.
Q 32). What will happen if you try to compile and run the following code ?//Constructor--Level1
class Test
{
int x;
Test(int n)
{
System.out.println(x=n); // line 6
}
D. Run-time exception
Correct Answer : OPTION B, Compilation error at line 10. Java compiler will throw an error becuase it
does not find default constructor in class Test.
As class Test has a parameterised constructor. So default constructor will not be autoamtically
created.
Q 33). Which type of exception will be thrown when you run this code ?// Exception---Level1
int a = 100, b = 0;
int c = a/b;
A. java.lang.Exception
B. java.lang.Throwable
C. java.lang.DivideByZeroException
D. java.lang.ArithmeticException
A. Error
B. MyException
C. RuntimeException
Correct Answer : OPTION B, MyException. Any method capable of causing exceptions must list all the
exceptions possible during its execution,
so that anyone calling that method gets a prior knowledge about which exceptions to handle. A
method can do so by using the throws keyword.
try
{
int arr[]={1,2};
arr[2]=3/0;
System.out.println(a[0]);
}
catch(Exception e)
{
System.out.println("Exception");
}
catch(ArithmeticException e)
{
System.out.println("Divide by Zero");
}
A. Exception is printed
C. compilation error
D. 1 is printed
Correct Answer : OPTION C, Compilation Error. While using multiple catch statements,
it is important to remember that exception sub-classes inside catch block must come before any of
their super-classes otherwise
it will lead to compile time error. Here catch(ArithmeticException e) is unreachable code which will
give an compilation error
Q 36). What will be the Output of the given program?// Overriding ---Level1
import java.io.*;
class Super
{
void show()
{ System.out.println("parent"); }
}
A. Compilation error
B. child
C. parent
D. None of above
Correct Answer : OPTION A, Compilation error. If super class method does not declare any
exception,
then sub class overriden method cannot declare checked exception but it can declare unchecked
exceptions.
A) 12 B) 1 C) 13 D) 123 E) Other
Answer: B 1
2) What will be the output of this program?//Basics ---Level2
public class Question3 {
public static void main(String[] args) {
System.out.println(hello("Test"));
}
private static String hello(String name) {
try {
return "Hello";
} finally {
return name;
}
}
}
A) Hello B) Hello Test C) Test D) Other
Answer: C - Test
A) 1 B) 0 C) 9 D) 10 E) other
Answer: D - 10
6) What will be the output of this program?// Inheritance—Level2
class Vehicle{
int maxSpeed = 100;
public String run() {return "Vehicle"; }
}
class Car extends Vehicle{
int maxSpeed = 200;
public String run() {return "Car";}
}
public class OverridingExample2 {
public static void main(String[] args) {
Vehicle a = new Car();
System.out.println(a.run()+" "+a.maxSpeed);
}
}
A) Car 100 B)Vehicle 100 C) Car 200 D) Vehicle 200 E) Compilation Error
Answer: A - Car 100
7) What will be the output of this program?//Polymorphism—Level3
class ClassA{
public static String getMessage() {return "Hello";}
public String getName() {return "ClassA";}
}
A) 3 B) 5 C) 0 D) 4 E) Other
Answer: B - 5
9) What will be the output of this program?//Collection—Level5
class Employee{
private int id; private String name;
Employee(int id, String name){
this.id = id;
this.name = name;
}
}
public class ArrayListRemoveDemo {
public static void main(String[] args) {
List<String> countries = new ArrayList<String>();
countries.addAll(Arrays.asList("Australia","Canada","India","USA"));
countries.remove(new String("USA"));
System.out.print(countries.size());
List<Employee> empList = new ArrayList<Employee>();
empList.add(new Employee(1,"A"));
empList.add(new Employee(1,"B"));
empList.add(new Employee(1,"C"));
empList.remove(new Employee(1,"A"));
System.out.print(empList.size());
}
}
A) 43 B) 32 C) 33 D) ArrayIndexOutOfBoundException
Answer: C - 33
11) What will be the output of this program?// collection --- level2
public class ArrayListDemo1 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(10);
list.add(10);
System.out.print(list.size());
list.remove(new Integer(10));
System.out.print(list.size());
}
}
A) 10 B) 20 C) 11 D) 21 E) IndexOutOfBoundsException
Answer: D - 21
12) What will be the output of this program? // collection---level2
public class Demo {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
Integer[] arr = {2,10,3};
list = Arrays.asList(arr);
list.set(0, 3);
System.out.print(list);
list.add(1);
System.out.print(list);
}
}
Explanation: Constructors does not have any return type, not even void.
15)Write code to find the First non repeated character in the String ?