Practice Core Java and Advanced Java MCQs
Practice Core Java and Advanced Java MCQs
enum EnumDemo {
A // line2
}
class Test {
enum EnumD { B }
void my_method()
{
enum EnumC { D } // line8
}
}
(a) Equal
(b) Not Equal
(c) Compilation error
(d) None of the above
3. What will happen when you attempt to compile and run the following class?
class Base {
Base(int var)
{
System.out.println("Base");
}
}
class Derived extends Base
{
public static void main(String argv[])
{
Derived obj = new Derived();
}
}
5. try
{
int number = Integer.parseInt("two");
}
Which could be used to create an appropriate catch block?
(a) ClassCastException
(b) IllegalStateException
(c) NumberFormatException
(d) None of the above
1. When access modifier is omitted from the definition of the member of a class. The member has
_________ access.
(a) default
(b) public
(c) private
(d) protected
2. The Scanner class is found in ______ package.
(a) java.lang
(b) java.util
(c) java.io
(d) None
1. class Shape
{
final public double calArea(){}
}
public class Circle extends Shape{
int radius;
public Circle(int radius)
{
this.radius = radius;
}
public double calArea()
{
return 3.142*radius*radius;
}
public static void main(String []args)
{
Shape obj = new Circle(5);
System.out.println(obj.calArea());
}
}
What will be the output of above code?
2. If a class inheriting an abstract class does not define all of its function then it will be known as
______.
(a) abstract
(b) a simple class
(c) static class
(d) interface
(a) abc
(b) abcd
(c) abcde
(d) compilation fails
4. Which among the following Sets maintains insertion order?
(a) HashSet
(b) TreeSet
(c) LinkedHashSet
(d) Both B & C
1. public class MyFirst {
2. public static void main(String[] args) {
3. MyFirst obj = new MyFirst(n);
4. }
5. static int a = 10;
6. static int n;
7. int b = 5;
8. int c;
9. public MyFirst(int m) {
10. System.out.println(a + ", " + b + ", " + c + ", " + n + ", " + m);
11. }
12. // Instance Block
13. {
14. b = 30;
15. n = 20;
16. }
17. // Static Block
18. static
19. {
20. a = 60;
21. }
22. }
a. 10, 30, 20
b. 60, 5, 0, 20
c. 10, 5, 0, 20, 0
d. 60, 30, 0, 20, 0
++z + y - y + z + x++
a. 24
b. 23
c. 20
d. 25
public class Test2 {
public static void main(String[] args) {
StringBuffer s1 = new StringBuffer("Complete");
s1.setCharAt(1,'i');
s1.setCharAt(7,'d');
System.out.println(s1);
}
}
a. Complete
b. Iomplede
c. Cimpletd
d. Coipletd
5. Which of the following modifiers can be used for a variable so that it can be accessed by any
thread or a part of a program?
a. global
b. transient
c. volatile
d. default
1. What should be the execution order, if a class has a method, static block, instance block, and
constructor, as shown below?
public class First_C {
public void myMethod()
{
System.out.println("Method");
}
{
System.out.println(" Instance Block");
}
public void First_C()
{
System.out.println("Constructor ");
}
static {
System.out.println("static block");
}
public static void main(String[] args) {
First_C c = new First_C();
c.First_C();
c.myMethod();
}
}
a. Object
b. int
c. long
d. void
3. Given that Student is a class, how many reference variables and objects are created by the
following code?
Student studentName, studentId;
studentName = new Student();
Student stud_class = new Student();
a. Map m = hashMap.synchronizeMap();
b. HashMap map =hashMap.synchronizeMap();
c. Map m1 = Collections.synchronizedMap(hashMap);
d. Map m2 = Collection.synchronizeMap(hashMap);
5. What will be the output of the following program?
abstract class MyFirstClass
{
abstract num (int a, int b) { }
}
a. No error
b. Method is not defined properly
c. Constructor is not defined properly
d. Extra parentheses
1. What is meant by the classes and objects that dependents on each other?
a. Tight Coupling
b. Cohesion
c. Loose Coupling
d. None of the above
2. Given,
int values[ ] = {1,2,3,4,5,6,7,8,9,10};
for(int i=0;i< Y; ++i)
System.out.println(values[i]);
a. 10
b. 11
c. 15
d. None of the above
3. What is the result of the following program?
public static synchronized void main(String[] args) throws
InterruptedException {
Thread f = new Thread();
f.start();
System.out.print("A");
f.wait(1000);
System.out.print("B");
}
public class Test {
public static void main(String[] args) {
int count = 1;
while (count <= 15) {
System.out.println(count % 2 == 1 ? "***" : "+++++");
++count;
} // end while
} // end main
}
a. 15 times ***
b. 15 times +++++
c. 8 times *** and 7 times +++++
d. Both will print only once
5. What will be the output of the following program?