Global logic Java bits- on basics 25
Global logic Java bits- on basics 25
System.out.println(arrList);
}
}
Output
[1, 1, 1]
## Q. What is the output of following program?
public class Test {
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. 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 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
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;
ANS. A
Q. In Java, can a constructor call itself recursively using the this() keyword?
ANS. B
Q. Which of the following statements is true regarding the use of this() and super() together
in a constructor?
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
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
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
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