MCQ Java Qustions
MCQ Java Qustions
MCQ Java Qustions
A. weakly typed
B. strongly typed
C. moderate typed
D. None of these
Answer: Option B
class A
a) Runtime Error
b) Compilation Error
c) It runs successfully
d) EnumNotDefined Exception
Answer: b
enum Enums
{
A, B, C;
private Enums()
{
System.out.println(10);
}
}
a) 10
10
10
b) Compilation Error
c) 10
10
d) Runtime Exception
Answer: a
Explanation: The constructor of Enums is called which prints 10.
a) Object
b) Enums
c) Enum
d) EnumClass
Answer: c
class selection_statements
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(++var2);
a) 1
b) 2
c) 3
d) 4
Answer:b
class jump_statments
int x = 2;
int y = 0;
if (y % x == 0)
continue;
else if (y == 8)
break;
else
a) 1 3 5 7
b) 2 4 6 8
c) 1 3 5 7 9
d) 1 2 3 4 5 6 7 8 9
Answer: c
class Output
int a = 5;
int b = 10;
first:
{
second:
third:
if (a == b >> 1)
break second;
System.out.println(a);
System.out.println(b);
a) 5 10
b) 10 5
c) 5
d) 10
Answer: d
8. Which of these keywords is used to prevent content of a variable from being modified?
a) final
b) last
c) constant
d) static
View Answer
Answer: a
class access
public int x;
static int y;
x += a ;
y += b;
class static_specifier
obj1.x = 0;
obj1.y = 0;
obj1.cal(1, 2);
obj2.x = 0;
obj2.cal(2, 3);
a) 1 2
b) 2 3
c) 3 2
d) 1 5
Answer: d
class Output
a) 1 2
b) 1 2 3
c) 1 2 3 4
d) 1 2 3 4 5
Answer: b
11. Which of these method of String class can be used to test to strings for equality?
a) isequal()
b) isequals()
c) equal()
d) equals()
View Answer
Answer: d
class string_class
a) false false
b) true true
c) true false
d) false true
Answer: d
a) True
b) False
Answer: b
class Alligator
int [][]y = x;
System.out.println(y[2][1]);
a) 2
b) 3
c) 7
d) Compilation Error
Answer: c
15. Which of the following keyword is used by calling function to handle exception thrown by called
function?
a) throws
b) throw
c) try
d) catch
Answer: a
try
finally
System.out.print("World");
a) Hello
b) World
c) Compilation Error
Answer: d
class exception_handling
{
try
int i, sum;
sum = 10;
catch(ArithmeticException e)
System.out.print("0");
System.out.print(sum);
a) 0
b) 05
c) Compilation Error
d) Runtime Error
Answer: c
class exception_handling
{
try
System.out.print(a[i]);
int x = 1/0;
catch(ArrayIndexOutOfBoundsException e)
System.out.print("A");
catch(ArithmeticException e)
System.out.print("B");
a) 12345
b) 12345A
c) 12345B
d) Compilation Error
Answer: c
19. Which of these access specifiers can be used for a class so that it’s members can be accessed by a
different class in the different package?
a) Public
b) Protected
c) Private
d) No Modifier
Answer: a
package pkg;
class display
int x;
void show()
if (x > 1)
class packages
for(int i=0;i<3;i++)
arr[i]=new display();
arr[0].x = 0;
arr[1].x = 1;
arr[2].x = 2;
for (int i = 0; i < 3; ++i)
arr[i].show();
a) 0
b) 1
c) 2
d) 0 1 2
Answer: c
a) Statement
b) PreparedStatement
c) CallableStatment
d) CalledStatement
Answer: c
a) rollback()
b) rollforward()
c) deleteTransaction()
d) RemoveTransaction()
Answer: a
d) start() method creates new thread and calls code written in run() method
Answer: d
24. Which of the following will ensure the thread will be in running state?
a) yield()
b) notify()
c) wait()
d) Thread.killThread()
Answer: c
interface calculate
int VAR = 0;
int x;
if (item<2)
x = VAR;
else
x = item * item;
}
class interfaces
for(int i=0;i<3;i++)
arr[i]=new display();
arr[0].cal(0);
arr[1].cal(1);
arr[2].cal(2);
a) 0 1 2
b) 0 2 4
c) 0 0 4
d) 0 1 4
Answer: c
26. What would be the output of following snippet, if attempted to compile and execute?
class abc
{
if(args.length>0)
System.out.println(args.length);
d) The snippet compiles and runs but does not print anything
Answer: d
27. Which concept of Java is achieved by combining methods and attribute into a class?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction
Answer: a
class Output
System.out.print(x);
a) 0
b) 1
c) 256
d) 257
Answer: b
29. Which tag should be used to pass information from JSP to included JSP?
Answer: a
a) Request
b) HttpRequest
c) HttpServletRequest
d) ServletRequest
Answer: c
a) id, class
b) id, type
c) type, property
d) type,id
Answer: a
a) <%@directive%>
b) <%!directive%>
c) <%directive%>
d) <%=directive%>
Answer: a
33, Which of these method of Thread class is used to Suspend a thread for a period of time?
a) sleep()
b) terminate()
c) suspend()
d) stop()
Answer: a
class multithreaded_programing
Thread t = Thread.currentThread();
t.setName("New Thread");
System.out.println(t.getName());
}
a) main
b) Thread
c) New Thread
d) Thread[New Thread,5,main].
Answer: c
35. Which of the following operators is used to generate instance of an exception which can be thrown
using throw?
a) thrown
b) alloc
c) malloc
d) new
Answer: d
SOLUTION:
public class Exercise24 {
String str = "The quick brown fox jumps over the lazy dog.";
SOLUTION:
import java.util.*;
public class Main {
public static void main(String[] args) {
String str1 = "w3resource";
System.out.println("The given string is: " + str1);
System.out.println("After removing duplicates characters the new
string is: " + removeDuplicateChars(str1));
}
private static String removeDuplicateChars(String sourceStr) {
char[] arr1 = sourceStr.toCharArray();
String targetStr = "";
for (char value: arr1) {
if (targetStr.indexOf(value) == -1) {
targetStr += value;
}
}
return targetStr;
}
}
SOLUTION
import java.util.*;
public class Main {
static final int N = 256;
static char MaxOccuringChar(String str1) {
int ctr[] = new int[N];
int l = str1.length();
for (int i = 0; i < l; i++)
ctr[str1.charAt(i)]++;
int max = -1;
char result = ' ';
return result;
}
public static void main(String[] args) {
String str1 = "test string";
System.out.println("The given string is: " + str1);
System.out.println("Max occurring character in the given string is:
" + MaxOccuringChar(str1));
}
}
Expected Output
First 10 Happy numbers:
1
7
10
13
19
23
28
31
SOLUTION
import java.util.HashSet;
public class Example9 {
public static void main(String[] args){
System.out.println("First 10 Happy numbers:");
for(long num = 1,count = 0;count<8;num++){
if(happy_num(num)){
System.out.println(num);
count++;
}
}
}
public static boolean happy_num(long num){
long m = 0;
int digit = 0;
HashSet<Long> cycle = new HashSet<Long>();
while(num != 1 && cycle.add(num)){
m = 0;
while(num > 0){
digit = (int)(num % 10);
m += digit*digit;
num /= 10;
}
num = m;
}
return num == 1;
}
}