Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

MCQ Java Qustions

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 23

JAVA MCQ QUESTIONS TEST – 17/9/2018

1.Java is a ........... language.

A. weakly typed

B. strongly typed

C. moderate typed

D. None of these

Answer: Option B

2. What is the output of below code snippet?

class A

enum Enums extends A

ABC, BCD, CDE, DEF;

a) Runtime Error

b) Compilation Error

c) It runs successfully

d) EnumNotDefined Exception

Answer: b

Explanation: Enum types cannot extend class.


3. What is the output of below code snippet?

enum Enums
{
A, B, C;

private Enums()
{
System.out.println(10);
}
}

public class MainClass


{
public static void main(String[] args)
{
Enum en = Enums.B;
}
}

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.

4. Which class does all the Enums extend?

a) Object

b) Enums

c) Enum

d) EnumClass

Answer: c

5. What is the output of this program?

class selection_statements
{

public static void main(String args[])

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

6. What is the output of this program?

class jump_statments

public static void main(String args[])

int x = 2;

int y = 0;

for ( ; y < 10; ++y)


{

if (y % x == 0)

continue;

else if (y == 8)

break;

else

System.out.print(y + " ");

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

7. What is the output of this program?

class Output

public static void main(String args[])

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

9. What is the output of this program?

class access

public int x;

static int y;

void cal(int a, int b)

x += a ;

y += b;

class static_specifier

public static void main(String args[])

access obj1 = new access();

access obj2 = new access();

obj1.x = 0;

obj1.y = 0;

obj1.cal(1, 2);

obj2.x = 0;

obj2.cal(2, 3);

System.out.println(obj1.x + " " + obj2.y);


}

a) 1 2

b) 2 3

c) 3 2

d) 1 5

Answer: d

10. What is the output of this program?

class Output

public static void main(String args[])

int arr[] = {1, 2, 3, 4, 5};

for ( int i = 0; i < arr.length - 2; ++i)

System.out.println(arr[i] + " ");

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

12. What is the output of this program?

class string_class

public static void main(String args[])

String obj = "hello";

String obj1 = "world";

String obj2 = "hello";

System.out.println(obj.equals(obj1) + " " + obj.equals(obj2));

a) false false

b) true true

c) true false

d) false true

Answer: d

13.Abstract class cannot have a constructor.

a) True

b) False
Answer: b

14. What is the output of this program?

class Alligator

public static void main(String[] args)

int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};

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

16. What is the output of this program?


class exception_handling

public static void main(String args[])

try

System.out.print("Hello" + " " + 1 / 0);

finally

System.out.print("World");

a) Hello

b) World

c) Compilation Error

d) First Exception then World

Answer: d

17. What is the output of this program?

class exception_handling

public static void main(String args[])

{
try

int i, sum;

sum = 10;

for (i = -1; i < 3 ;++i)

sum = (sum / i);

catch(ArithmeticException e)

System.out.print("0");

System.out.print(sum);

a) 0

b) 05

c) Compilation Error

d) Runtime Error

Answer: c

18. What is the output of this program?

class exception_handling

public static void main(String args[])

{
try

int a[] = {1, 2,3 , 4, 5};

for (int i = 0; i < 5; ++i)

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

20. What is the output of this program?

package pkg;

class display

int x;

void show()

if (x > 1)

System.out.print(x + " ");

class packages

public static void main(String args[])

display[] arr=new display[3];

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();

Note : packages.class file is in directory pkg;

a) 0

b) 1

c) 2

d) 0 1 2

Answer: c

21. Which of the following is used to call stored procedure?

a) Statement

b) PreparedStatement

c) CallableStatment

d) CalledStatement

Answer: c

22. Which of the following is used to rollback a JDBC transaction?

a) rollback()

b) rollforward()

c) deleteTransaction()

d) RemoveTransaction()

Answer: a

23. What is true about threading?

a) run() method calls start() method and runs the code

b) run() method creates new thread


c) run() method can be called directly without start() method being called

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

25. What is the output of this program?

interface calculate

int VAR = 0;

void cal(int item);

class display implements calculate

int x;

public void cal(int item)

if (item<2)

x = VAR;

else

x = item * item;
}

class interfaces

public static void main(String args[])

display[] arr=new display[3];

for(int i=0;i<3;i++)

arr[i]=new display();

arr[0].cal(0);

arr[1].cal(1);

arr[2].cal(2);

System.out.print(arr[0].x+" " + arr[1].x + " " + arr[2].x);

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
{

public static void main(String args[])

if(args.length>0)

System.out.println(args.length);

a) The snippet compiles, runs and prints 0

b) The snippet compiles, runs and prints 1

c) The snippet does not compile

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

28. What is the output of this program?

class Output

public static void main(String args[])

Integer i = new Integer(257);


byte x = i.byteValue();

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?

a) Using <%jsp:page> tag

b) Using <%jsp:param> tag

c) Using <%jsp:import> tag

d) Using <%jsp:useBean> tag

Answer: a

30. “request” is instance of which one of the following classes?

a) Request

b) HttpRequest

c) HttpServletRequest

d) ServletRequest

Answer: c

31. Which is mandatory in tag?

a) id, class

b) id, type

c) type, property
d) type,id

Answer: a

32. Which one of the following is correct for directive in JSP?

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

34. What is the priority of the thread in output of this program?

class multithreaded_programing

public static void main(String args[])

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

JAVA LOGIC BUILDING QUESTIONS TEST DATE 17/9/2018


1 . Write a Java program to replace all the 'd' characters with 'f'
characters

SOLUTION:
public class Exercise24 {

public static void main(String[] args)

String str = "The quick brown fox jumps over the lazy dog.";

// Replace all the 'd' characters with 'f' characters.

String new_str = str.replace('d', 'f');

// Display the strings for comparison.


System.out.println("Original string: " + str);

System.out.println("New String: " + new_str);

2. Write a Java program to print after removing duplicates from a


given string

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;
}
}

3. Write a Java program to find the maximum occurring character


in a string

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 = ' ';

for (int i = 0; i < l; i++) {


if (max < ctr[str1.charAt(i)]) {
max = ctr[str1.charAt(i)];
result = str1.charAt(i);
}
}

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));
}
}

4. Write a Java program to find and print the first 10 happy


numbers.

Happy number: Starting with any positive integer, replace the


number by the sum of the squares of its digits, and repeat the
process until the number equals 1, or it loops endlessly in a cycle
which does not include 1.
Example: 19 is a happy number
12 + 92=82
82 + 22=68
62 + 82=100
12 + 02 + 02=1

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;
}
}

You might also like