Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

Global logic Java bits- on basics 25

Uploaded by

akankshyatarenia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Global logic Java bits- on basics 25

Uploaded by

akankshyatarenia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Global logic Java bits

# Java Multiple Choice Questions


## Q. Which of the following below live on the heap in java?
* Class
* Instance variable
* Method
* Object
Ans. Object
## Q. Which of these interface handle sequences?
* Set
* List
* Comparator
* Collection
Ans. List
## Q. Which of this interface must contain a unique element?
* Set
* List
* Array
* Collection
Ans. Set
## Q. Which of the following declarations does not compile?
A. double num1, int num2 = 0;
B. int num1, num2;
C. int num1, num2 = 0;
D. int num1 = 0, num2 = 0;
A. double num1, int num2 = 0;
## Q. What is the output of following program?
public class Test {

public static void main(String[] args) {


for(int i = 0; i < 5; i++) {
System.out.println(i +” “);
}
}
}
Output
1
2
3
4
5
## Q. What is the output of following program?
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList arrList = new ArrayList();
arrList.add(1);
arrList.add('1');
arrList.add("1");

System.out.println(arrList);
}
}
Output
[1, 1, 1]
## Q. What is the output of following program?
public class Test {

public static void main(String[] args) {


for(int i=0; 0; i++) {
System.out.println("Hello World!");
}
}
}
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from int to boolean

at Test.main(Test.java:5)
## Q. Which statement about a valid .java file is true?
**A.** It can only contain one class declaration.
**B.** It can contain one pulic class declaration and one public interface definition.
**C.** It must define at least one public class.
**D.** It may define at most one public class.
Answer: D
## Q. What is the output of following program?
public class Test {
private static int one = 10;
int two = 20;
public static void main(String []args) {
Test test = new Test();
int today = 20; two = 40;
System.out.println(today + test.two + test.one);
}
}
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static field two
at Test.main(Test.java:9)

## Q. What is the output of following program?


public class Test{
static int start = 2;
final int end;
public Test(int x) {
x = 4;
end = x;
}
public void fly(int distance) {
System.out.println(end-start+" ");
System.out.println(distance);
}
public static void main(String []args){
new Test(10).fly(5);
}
}
Output
[2 5]
## Q. What is the output of following program?
public class Test
{
public static void main(String a[]) {
try {
int val = 10/0;
} catch(Exception e) {
System.out.println(e);
} catch(ArithmeticException ae) {
System.out.println(ae);
}
}
}
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable catch block for ArithmeticException. It is already handled by the catch
block for Exception at Test.main(Test.java:12)
## Q. What is the output of following program?
public class Test {
static void charNum(String inputString) {
HashMap<Character, Integer> charMap = new HashMap<Character, Integer>();
char[] strArray = inputString.toCharArray();
for(char c: strArray) {
if(charMap.containsKey(c)) {
charMap.put(c, charMap.get(c)+1);
} else {
charMap.put(c, 1);
}
}
Set<Character> charInString = charMap.keySet();
for(Character ch: charInString) {
if(charMap.get(ch) > 1) {
System.out.println(ch +" : "+ charMap.get(ch));
}
}
}
public static void main(String[] args) {
charNum("JavaJ2Ee");
}
}
Output
a:2
J:2
## Q. Which of the following declarations does not compile?
A. double num1, int num2 = 0;
B. int num1, num2;
C. int num1, num2 = 0;
D. int num1 = 0, num2 = 0;
A. double num1, int num2 = 0;
**Explanation**: A. Option A does not compile because Java does not allow declaring
different types as part of the same declaration.
## Q. What is the output of the following?
public static void main(String... args) {
String chair, table = "metal";
chair = chair + table;
System.out.println(chair);
}
A. metal
B. metalmetal
C. nullmetal
D. The code does not compile
D. The code does not compile
**Explanation**: The local variable chair may not have been initialized

## Q. Which is correct about an instance variable of type String?


A. It defaults to an empty string.
B. It defaults to null.
C. It does not have a default value.
D. It will not compile without initializing on the declaration line
```
B. It defaults to null.
## Q. How many of the following methods compile?
public class Test {
public String convert(int value) {
return value.toString();
}
public String convert(Integer value) {
return value.toString();
}
public String convert(Object value) {
return value.toString();
}

public static void main(String... args) {


Test obj = new Test();
System.out.println(obj.convert(10));
}
}
A. None
B. One
C. Two
D. Three
C. Two
**Explanation**: Objects have instance methods while primitives do not. Since int is a
primitive, you cannot call instance methods on it. Integer and String are both objects and
have instance methods.
Q. Output of the following?
class variable_scope
{
public static void main(String args[])
{
int x;
x = 5;
{
int y = 6;
System.out.print(x + " " + y);
}
System.out.println(x + " " + y);
}
}
a) Compilation error
b) Runtime error
c) 5 6 5 6
d) 5 6 5
ANS .A
## Q. Which is the first line to trigger a compiler error?
double d1 = 5f; // p1
double d2 = 5.0; // p2
float f1 = 5f; // p3
float f2 = 5.0; // p4
A. p1
B. p2
C. p3
D. p4
D. P4
**Explanation**: Type mismatch: cannot convert from double to float

## Q. How many instance initializers are in this code?


public class Bowling { {
System.out.println();
}
public Bowling () {
System.out.println();
}
static {
System.out.println();
}
{
System.out.println();
}
}
A. None
B. One
C. Two
D. Three
C. Two
## Q. What is true of the finalize() method?
A. It may be called zero or one times.
B. It may be called zero or more times.
C. It will be called exactly once.
D. It may be called one or more times.
A. It may be called zero or one times.
**Explanation**: The `finalize()` method may not be called, such as if your program crashes.
However, it is guaranteed to be called no more than once.

## Q. Which of the following is true about primitives?


A. You can call methods on a primitive.
B. You can convert a primitive to a wrapper class object simply by assigning it.
C. You can convert a wrapper class object to a primitive by calling valueOf().
D. You can store a primitive directly into an ArrayList.
B. You can convert a primitive to a wrapper class object simply by assigning it.
## Q. What is the output of the following?
Integer integer = new Integer(4);
System.out.print(integer.byteValue());
System.out.print("-");
int i = new Integer(4);
System.out.print(i.byteValue());
A. 4-0
B. 4-4
C. The code does not compile.
D. The code compiles but throws an exception at runtime
C. The code does not compile.

## Q. Which two primitives have wrapper classes that are not merely the name of the
primitive with an uppercase letter?
A. byte and char
B. byte and int
C. char and int
D. None of the above
C. char and int
**Explanation**: The wrapper class for int is Integer and the wrapper class for char is
Character. All other primitives have the same name. For example, the wrapper class for
boolean is Boolean.

## Q. How do you force garbage collection to occur at a certain point?


A. Call System.forceGc()
B. Call System.gc()
C. Call System.requireGc()
D. None of the above
D. None of the above
**Explanation**: While you can suggest to the JVM that it might want to run a garbage
collection cycle, the JVM is free to ignore your suggestion.

## Q. How many of the String objects are eligible for garbage collection right before the end
of the main method?
public static void main(String[] fruits) {
String fruit1 = new String("apple");
String fruit2 = new String("orange");
String fruit3 = new String("pear");
fruit3 = fruit1;
fruit2 = fruit3;
fruit1 = fruit2;
}
A. None
B. One
C. Two
D. Three
C. Two
**Explanation**: All three references point to the String apple. This makes the other two
String objects eligible for garbage collection.
## Q. Which of the following is the output of this code, assuming it runs to completion?
public class Toy {
public void play() {
System.out.print("play-");
}
public void finalizer() {
System.out.print("clean-");
}
public static void main(String[] fun) {
Toy car = new Toy();
car.play();
System.gc();
Toy doll = new Toy();
doll.play();
}
}
A. play-
B. play-play-
C. play-clean-play-
D. play-play-clean-clean-
B. play-play-
**Explanation**: If there was a finalize() method, this would be a different story. However,
the method here is finalizer. Tricky! That’s just a normal method that doesn’t get called
automatically. Therefore clean is never output.

## Q. What is the value of tip after executing the following code snippet?
int meal = 5;
int tip = 2;
int total = meal + (meal>6 ? ++tip : --tip);
A. 1
B. 2
C. 3
D. 6
D. 6
**Explanation**: In ternary expressions, only one of the two right-most expressions are
evaluated. Since `meal>6` is false, `––tip` is evaluated and `++tip` is skipped. `tip` is changed
from 2 to 1 and `total` becomes `meal + (1)` which means `5 + 1 = 6`.
## Q. What is the output of the following application?
String john = "john";
String jon = new String(john);
System.out.println((john==jon) + " "+ (john.equals(jon)));
A. true true
B. true false
C. false true
D. false false
C. false true

##Predict the output?


class T {
int t = 20;
}
class Main {
public static void main(String args[]) {
T t1 = new T();
System.out.println(t1.t);
}
}
a.20
b.0
c. Compiler Error
d. None of the above
Ans. 20
## Which of the following is/are true about constructors in Java?

1) Constructor name should be same as class name.


2) If you don't define a constructor for a class,
a default parameterless constructor is automatically
created by the compiler.
3) The default constructor calls super() and initializes all
instance variables to default value like 0, null.
4) If we want to parent class constructor, it must be called in
first line of constructor.
(A) 1
(B) 1, 2
(C) 1, 2 and 3
(D) 1, 2, 3 and 4

Ans. D
##Is there any compiler error in the below Java program?
class Point {
int m_x, m_y;
public Point(int x, int y) { m_x = x; m_y = y; }
public static void main(String args[]) {
Point p = new Point();
}
}
Yes
No
Ans. Yes
Q.Output of following Java program
class Point {
int m_x, m_y;

public Point(int x, int y) { m_x = x; m_y = y; }


public Point() { this(10, 10); }
public int getX() { return m_x; }
public int getY() { return m_y; }

public static void main(String args[]) {


Point p = new Point();
System.out.println(p.getX());
}
}
a.10
b.0
c. compilation error
d. None of the above
Ans. 10

Q. What is the output of the following?


final class Complex {
private double re, im;
public Complex(double re, double im) {
this.re = re;
this.im = im;
}
Complex(Complex c)
{
System.out.println("Copy constructor called");
re = c.re;
im = c.im;
}
public String toString() {
return "(" + re + " + " + im + "i)";
}
}
class Main {
public static void main(String[] args) {
Complex c1 = new Complex(10, 15);
Complex c2 = new Complex(c1);
Complex c3 = c1;
System.out.println(c2);
}
}
A. Copy constructor called
(10.0 + 15.0i)
B.Copy constructor called
C.(0.0 + 0.0i)
D. (10.0 + 15.0i)
Ans. A
Q. What is the output of the following?
class Test{
static int a;
static{
a = 4;
System.out.println ("inside static blockn");
System.out.println ("a = " + a);
}
Test(){
System.out.println ("ninside constructorn");
a = 10;
}
public static void func(){
a = a + 1;
System.out.println ("a = " + a);
}
public static void main(String[] args){
Test obj = new Test();
obj.func();
}
}
A.inside static block
a=4
inside constructor
a = 11
B.Compiler Error
C.Run Time Error
D.inside static block
a=4
inside constructor
a=5
inside static block
a = 10
inside constructor
a = 11

ANS. A

Q. Constructors can be marked as “static.”


True
False
Ans.False
Q. Which of the following is true about the order of constructor invocation during object
creation in Java?
A) Subclass constructors are invoked before superclass constructors.
B) Constructors of all classes are invoked simultaneously.
C) Superclass constructors are invoked before subclass constructors.
D) Constructor invocation order is random.
Ans. C

Q. In Java, can a constructor call itself recursively using the this() keyword?

A) Yes, it is a common practice for constructor initialization.


B) Yes, but only if the constructor is marked as “private.”
C) No, recursive constructor calls using this() are not allowed.
D) No, constructors cannot use this() keyword.

ANS. B
Q. Which of the following statements is true regarding the use of this() and super() together
in a constructor?

A) Both this() and super() can be used together in any constructor.


B) Only this() can be used to call another constructor within the same class.
C) Only super() can be used to call a constructor of the superclass.
D) Using both this() and super() together causes a compilation error.
ANS. C
Q) What is the default value of an instance variable (non-static field) of an object in Java if
not explicitly initialized?

A) 0
B) null
C) false
D) Depends on the data type
Ans. B
Q.Which access modifier allows a class member to be accessible within the same package
and by subclasses, even if they are in different packages?

A) private
B) protected
C) public
D) default (package-private)
Ans. B
Q. When does method overloading is determined?
A. At run time
B. At compile time
C. At coding time
D. At execution time
ANS. B
Q.Which concept of Java is a way of converting real world objects in terms of class?
A.Polymorphism
B.Encapsulation
C.Abstraction
D.Inheritance
ANS. B
Q.What is it called if an object has its own lifecycle and there is no owner?
A. Aggregation
B. Composition
C. Encapsulation
D. Association
Ans. A
Q.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

Q.What will be the output of the following Java 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
Ans. A
9. What will be the output of the following Java code?

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
Ans. C

Q. What will be the output of the following Java program?

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) 0 256
b) 0 64
c) 256 0
d) 64 0
ANS. C
Q. Which exception is thrown when java is out of memory?
a) MemoryError
b) OutOfMemoryError
c) MemoryOutOfBoundsException
d) MemoryFullException
Ans. B
Q. What will be the output of the following Java program?
class recursion {
int func (int n) {
int result;
if (n == 1)
return 1;
result = func (n - 1);
return result;
}
}
class Output {
public static void main(String args[]) {
recursion obj = new recursion() ;
System.out.print(obj.func(5));
}
}
a) 1
b) 120
c) 0
d) None of the mentioned
Ans. 1

Q. What will be the output of the following Java code?


class Output {
public static void main(String args[]) {
Integer i = new Integer(257);
byte x = i.byteValue();
System.out.print(x);
}
}
a) 257
b) 256
c) 1
d) 0
Ans. c

What will be the output of the following Java code?

class Output
{
public static void main(String args[])
{
double x = 3.14;
int y = (int) Math.ceil(x);
System.out.print(y);
}
}
a) 3
b) 0
c) 4
d) 3.0
Ans. 4

You might also like