Set B

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Q 1). What is the output of this program?

//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.

Q 2). What is difference between "abc".equals(unknown string) and unknown.equals("abc")?


//String ---Level1

A. First is safe for NullPointerException

B. Second is safe for NullPointerException

Correct Answer : OPTION A, First is safe for NullPointerException

Q 3). Which of the following stements are incorrect? // Basic ---Level1

A. Default constructor is called at the time of declaration of the object if a constructor has not been
defined.

B. Constructor can be parameterized.

C. finalize() method is called when a object goes out of scope and is no longer needed.

D. finalize() method must be declared protected.

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.

Thus, it cannot be accessed from outside the class.

Q 5). Which of the following statements are incorrect? //Basic --Level1

A. Variables declared as final occupy memory.

B. final variable must be initialized at the time of declaration.

C. Arrays in java are implemented as an object.

D. All arrays contain an attribute-length which contains the number of elements stored in the array.

Correct Answer : OPTION A, Variables declared as final occupy memory.

Q 6). Which of these is not a bitwise operator? //Basic operator---level1

A. &
B. &=

C. |=

D. <=

Correct Answer : OPTION D, <= Because this is a relational operator.

Q 7). What is the output of this program?// Basic operator--Level2

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.

42 in binary is 00101010 in using ~ operator on var1 and

assigning it to var2 we get inverted value of 42 i:e 11010101 which is -43 in decimal.

Q 8). What is the output of this program? //Operators---Level3

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

Correct Answer : OPTION D, 256 0

Q 9). How do you ensure that N thread can access N resources without deadlock? //Thread--- Level2

A. By acquiring resources in a particular order and release resources in reverse order.

B. By acquiring resources in a particular order and release resources in same order

C. By acquiring resources in a particular order and not releasing resources.

D. Not Possible

Correct Answer : OPTION B, By acquiring resources in a particular order and release resources in
same order.

Q 10). What is the output of this program? // Loops --Level3

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.

i gets the value 0,1,2,3 and j gets the values 0,2,3,4.

Q 11). Can you pass List<String> to a method which accepts List<Object>? //Collection -- Level2

A. Yes

B. No, but it wont give any error


C. This will lead to Compilation Error

D. This will lead to Runtime Error

Correct Answer : OPTION C, This will lead to Compilation Error.

At fist glance it looks like String is object so List<String> can be used where List<Object> is required

but this is not true. It will result in compilation error.

It does make sense if you go one step further because List<Object> can store anything including
String,

Integer etc but List<String> can only store Strings.

Q 12). What is the Output of this program?// Basic---Level1

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

C. Runtime error owing to division by zero

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

Correct Answer : OPTION C, java.util.TreeMap


Q 14) . In Runnable, many threads share the same object instance. True or False?//Thread ---Level2

A. True

B. False

Correct Answer : OPTION A, True

https://www.careerride.com/question-20-Java-Part-2

Q 15). Java beans have no types. True or False?// Beans---Level2

A. True

B. False

Correct Answer : OPTION A, True

Q 16). What is the output of this program?//Basic---Level1

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.

Q 18) . What is the output of this program?//Basic loops --Level2

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

condition of for loop.

Q 19). Which statement is static and synchronized in JDBC API?//JDBC static---level1

A. executeQuery()

B. executeUpdate()

C. getConnection()

D. prepareCall()

Correct Answer : OPTION C, getConnection()

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

Correct Answer : OPTION D, byte

Q 21). The class java.sql.Timestamp is associated with?// Date-- Level2

A. java.util.Time

B. java.sql.Time

C. java.util.Date

D. None of the above

Correct Answer : OPTION 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

declare a stack of characters;

while (there are more characters in the word to read)


{
read a character;
push the character on the stack;
}
while (the stack is not empty)
{
pop a character off the stack;
write the character to the screen;
}

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())

Q 23) . To evaluate an expression without any embedded function calls://DS---Level3

A. One stack is enough

B. Two stacks are needed

C. As many stacks as the height of the expression tree are needed

D. A Turing machine is needed in the general case

Correct Answer : OPTION A, One stack is enough. Any expression can be converted into Postfix or
Prefix form.

Prefix and postfix evaluation can be done using a single stack.

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

Correct Answer : OPTION C, 142

Q 25) . Suppose a stack is to be implemented using a linked list instead of an array.

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

A. O(1) for insertion and O(n) for deletion

B. O(1) for insertion and O(1) for deletion

C. O(n) for insertion and O(1) for deletion

D. O(n) for insertion and O(n) for deletion

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.

Q 26) . What will be the result of given code?// Strings--Level1

String str = "Java";


String str1 = new String("Java");

System.out.println(str.equals(str1));
System.out.println(str == str1);
System.out.println( str.compareTo(str1) );

A. true true true

B. true true 0

C. true false true

D. true false 0

Correct Answer : OPTION D

Q 27). What will be the result of given code?//Strings---Level2

String str = "StudyTonight";


str.concat(".com");
str = str.toUpperCase();
str.replace("TONIGHT","today");
System.out.println(str);

A. STUDYTONIGHT

B. STUDYtoday.COM

C. STUDYTONIGHT.COM

D. STUDYtoday

Correct Answer : OPTION A


Q 28). What will be the output of following code?// Strings --Level1

String str = "Java was developed by James Ghosling";


System.out.println(str.substring(19));

A. by James Ghosling

B. y James Ghosling

C. James Ghosling

D. Java was developed

Correct Answer : OPTION A

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.

Q 30). What will be the output of the following program?// Basic--Level1


class B
{
static int count = 100;
public void increment()
{
count++;
}
public static void main(String []args)
{
B b1 = new B();
b1.increment();
B b2 = new B();
System.out.println(b2.count); // line 13
}
}

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
}

public static void main(String []args)


{
Test n = new Test(); // line 10
}
}

A. Program exits without printing anything

B. Compilation error at line 10

C. Compilation error at 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

Correct Answer : OPTION D, java.lang.ArithmeticException object is thrown by Java Runtime System


for any type of arithmetic exception.
Q 34). What should replace XXXX ?// Exception--Level1

class MyException extends Exception


{
public void method() throws XXXX
{
throw new MyException();
}
}

A. Error

B. MyException

C. RuntimeException

D. throws clause isn't required

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.

Q 35). What will happen on running the following code ? // Exception---Level1

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

B. Divide by Zero 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

because all the exception is caught by catch(Exception e).

Q 36). What will be the Output of the given program?// Overriding ---Level1

import java.io.*;
class Super
{
void show()
{ System.out.println("parent"); }
}

public class Sub extends Super


{
void show() throws IOException
{ System.out.println("child"); }

public static void main( String[] args )


{
Super s = new Sub();
s.show();
}
}

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.

IOException is a checked exception.

37) What will be the output of this program?// Basic ---Level2


public class Question1 {
public static String getValue(Integer value) {
return "interger";
}
public static String getValue(Object value) {
return "object";
}
public static void main(String[] args) {
System.out.println(getValue(null));
}
}

A) integer B) object C) other D) Compile error for ambiguous method


Answer: A interger
1) What will be the output of this program?// Basic Exception---Level1
public class Question2 {
public static void main(String[] args) {
try {
System.out.println("1");
System.exit(0);
System.out.println("2");
} finally {
System.out.println("3");
}
}
}

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

3) What will be the output of this program?// NumberFormat---Level2


public class Question4 {
public static void main(String[] args) {
NumberFormat format = NumberFormat.getInstance();
try {
Number num = format.parse("123xyx456");
System.out.println(num);
} catch (ParseException E) {
e.printStackTrace();
}
}
}

A) Parse Exception B) 456 C) xyz D) 123


Answer: D - 123

4) What will be the output of this program?//Inheritance --- Level2


class ClassA{
public String getMessage() {
return "Hello";
}
}
interface InterfaceA{
default String getMessage() {
return "Hi";
}
}
public class Demo extends ClassA implements InterfaceA {
public static void main(String[] args) {
System.out.println(new Demo().getMessage());
}
}

A) Hi B) Hello C) Compilation Error D) other


Answer: B - Hello
5) What will be the output of this program?// Collection—Level3
public class ShortTest {
public static void main(String[] args) {
Set<Short> set = new HashSet<Short>();
for (Short i = 0; i < 10; i++) {
set.add(i);
set.remove(i - 1);
}
System.out.println(set.size());
}

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";}
}

class ClassB extends ClassA{


public static String getMessage() {return "Hi";}
public String getName() {return "ClassB";}
}

public class Demo {


public static void main(String[] args) {
ClassA a = new ClassB();
System.out.println(a.getMessage()+" "+a.getName());
}
}

A) Hello ClassA B) Hi ClassB C) Hello ClassB D) Hi ClassA E) Compilation Error


Answer: C - Hello ClassB

8) What will be the output of this program?// collection—level2


class Student {
int rollNumber;
Student(int n){
rollNumber = n;
}
}
public class Demo {
public static void main(String[] args) {
Set<Student> students = new HashSet<Student>();
students.add(new Student(1));
students.add(new Student(3));
students.add(new Student(4));
students.add(new Student(1));
students.add(new Student(3));
System.out.println(students.size());
}
}

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

10) What will be the output of this program?//Collection---Level3


public class ArrayListDemo {
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(10);
System.out.print(list.size());
}
}

A) 10 B) 21 C) 20 D) Print 1 followed by IndexOutOfBoundsException


E) Print 2 followed by IndexOutOfBoundsException
Answer: E - Print 2 followed by IndexOutOfBoundsException

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);
}
}

A) [3, 10, 3] ,followed by exception B) ) [2,10,3] ) [2,10,3,1]


C) [3,10,3] ) [3,10,3] D) [3,10,3] ) [3,10,3,1]
Answer: A - [3, 10, 3] ,followed by exception

14) What is the return type of Constructors? // constructors—level 1

A) int B) float C) void D) none of the mentioned


Answer: D

Explanation: Constructors does not have any return type, not even void.

15)Write code to find the First non repeated character in the String ?

You might also like