Java

Question 1

What does the following java function perform ? (Assume int occupies four bytes of storage)

Public static int f(int a)
{
           //Pre-conditions : a>0 and no overflow/underflow occurs
           int b=0;
           for(int i=0; i<32; i++)
           {
                   b=b <<1;
                   b= b | (a & 1);
                   a = a >>> 1; // This is a logical shift
           }
      Return b;
} 
A
Return the int that represents the number of 0’s in the binary representation of integer a.
B
Return the int that represents the number of 1’s in the binary representation of integer a.
C
Return the int that has the reversed binary representation of integer a.
D
Return the int that has the binary representation of integer a.
Question 1 Explanation: 
→ Consider the a value is 5.
The initial value of b is 0 and b<<1 means b will get value of 2
b=b|(a&1) which means 2|(5&1) then b will get value “2”
a=a<<1 means value a will reduce to 3
→ Repeat the above procedure for 31 iterations.
→ Iteration-2: b=b<<1 then b is 4 and b=b|(a&1)= 4|(3&1) then b is 5 and a becomes 1
→ Iteration-3: b=b<<1 then b is 10 and b=b|(1&1) = 4|(3&1) then b is 11 and a becomes 0
→ And this procedure will repeat until “i” value is 32.
→ The final value is 11 (Number of 1’s in the result is 2) and the number of 1’s in the input 5 are 2.
Question 2

Consider the following recursive Java function f that takes two long arguments and returns a float value :

public static float f(long m, long n)
{
      float result = (float) m / (float) n;
      if (m < 0 || n < 0)
               return 0⋅0f;
      else
               result += f(m*2, n*3);
      result result;
} 

Which of the following integers best approximates the value of f(2,3) ?

A
0
B
3
C
1
D
2
Question 3
The built-in base class in Java, which is used to handle all exceptions is
A
Raise
B
Exception
C
Error
D
Throwable
Question 3 Explanation: 
Throwable class is the built-in base class used to handle all the exceptions in Java.
Question 4
What is the right way to declare a copy constructor of a class if the name of the class is MyClass?
A
MyClass (constant MyClass *arg)
B
MyClass (constant MyClass &arg)
C
MyClass (MyClass arg)
D
MyClass (MyClass *arg)
Question 4 Explanation: 
→Copy Constructor is a type of constructor which is used to create a copy of an already existing object of a class type.
→Declaration of copy constructor syntax is different from c++ to java.
→In C++, It is usually of the form class-name (Class_name &vn),
→In Java, the syntax is Class_name(class_name vn).
→So w.r.to C++ , option (B) is correct and w.r.to Java option (c ) is correct.
Question 5
What is the output of the following Java program?
Class Test
{
public static void main (String [] args)
{
int x = 0;
int y = 0;
for (int z = 0; z < 5; z++)
{
if((++x > 2) || (++y > 2))
{
x++;
}
}
System.out.println( x + " " + y);
}
}
A
8 2
B
8 5
C
8 3
D
5 3
Question 5 Explanation: 
Initial values of x and y is 0
For the given code, the loop will execute 5 times.
Step-1: For z = 0,
The following condition will execute
if((++x > 2) || (++y > 2))
Here pre-increment operation will be performed on x and y. Then x and y values are 1 and the condition false
Similar operation will be performed for z values 1,2,3 and 4
Step-2: For z = 1, Then x and y values become 2 and the condition false.
Step-3: For z = 2, “||” operator is present in the expression, if first operand is true then no need to check the second operand.x value is 3, if condition true and ++y is not evaluated,again “x” value is incremented then x value is “4”.
Perform the step-3 procedure for z value 3 and 4 . Here x value is incremented “4” times. Then the final value of “x” is 8 and “y” is 2.
Question 6
Find the output of the following Java code line
System.out.println(math.floor(-7.4))
A
-7
B
-8
C
-7.4
D
-7.0
Question 6 Explanation: 
The method floor gives the largest integer that is less than or equal to the argument.
But here the argument is negative value.
Floor of -7.4 will return the lower limit, i.e. -8.
Question 7
In Java, after executing the following code what are the values of x, y and z?
int x,y=10; z=12;
x=y++ + z++;
A
x=22, y=10, z=12
B
x=24, y=10, z=12
C
x=24, y=11, z=13
D
x=22, y=11, z=13
Question 7 Explanation: 
x = y++ + z++;
Post increment operator first perform the action and then it increment the value.
First perform addition of ‘y’ and ‘z’ and later increment ‘y’ and ‘z’ values
So, the value of x = 10 + 12 = 22, y = 11 and z = 13.
Question 8
The feature in object-oriented programming that allows the same operation to be carried out differently, depending on the object, is:
A
Inheritance
B
Polymorphism
C
Over functioning
D
Overriding
Question 8 Explanation: 
In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object (prototype-based inheritance) or class (class-based inheritance), retaining similar implementation.

In any object-oriented programming language,Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes

Polymorphism is the provision of a single interface to entities of different types or the use of a single symbol to represent multiple different types
Question 9
We can make a class abstract by
A
Declaring it abstract using the virtual keyword
B
Making at least one member function as virtual function
C
Making at least one member function as pure virtual function
D
Making all member function const
Question 9 Explanation: 
An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. A pure virtual function can be declared by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration.
Question 10
Which of the following is FALSE about inheritance in Java?
A
Private methods are final
B
Protected members are accessible within a package and inherited classes outside the package
C
Protected methods are final
D
We cannot override private methods
Question 10 Explanation: 
→ Methods declared public in a superclass must also be public in all subclasses (this, by the way, is the reason most of the applet methods are public).
→ Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private.
→ Methods declared private are not inherited and therefore this rule doesn't apply.
→ Methods declared without protection at all (the implicit package protection) can be declared more private in subclasses.
Question 11
The best example of compile time polymorphism is:
A
Method declaration
B
Method overriding
C
Method overloading
D
Method Invoking
Question 11 Explanation: 
There are two types of polymorphism in java:
1) Static Polymorphism also known as compile time polymorphism
2) Dynamic Polymorphism also known as runtime polymorphism
→ Method Overloading allows us to have more than one method having the same name, if the parameters of methods are different in number, sequence and data types of parameters.
→ Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime, that's why it is called runtime polymorphism
Question 12
Which of these classes is not part of Java’s collection framework?
A
Map
B
Array
C
Arraylist
D
Vector
Question 12 Explanation: 
The java.util.Map interface represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface. Therefore it behaves a bit different from the rest of the collection types.
There are 12 questions to complete.

Access subject wise (1000+) question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now