0% found this document useful (0 votes)
268 views27 pages

7.MCQ With Ans

To read input from the user. 48. What is the difference between == and .equals() in Java? - == checks if two objects refer to the same instance (reference equality) - .equals() checks if two objects have the same value (value equality) So == checks reference equality while .equals() checks content equality. 49. What is the difference between an abstract class and an interface in Java? - Abstract class can have abstract and non-abstract methods while interface has only abstract methods. - Abstract class can have fields but interface cannot have fields, they are implicitly public, static and final. - Multiple inheritance is not supported in Java but a class can implement multiple interfaces

Uploaded by

Aman Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
268 views27 pages

7.MCQ With Ans

To read input from the user. 48. What is the difference between == and .equals() in Java? - == checks if two objects refer to the same instance (reference equality) - .equals() checks if two objects have the same value (value equality) So == checks reference equality while .equals() checks content equality. 49. What is the difference between an abstract class and an interface in Java? - Abstract class can have abstract and non-abstract methods while interface has only abstract methods. - Abstract class can have fields but interface cannot have fields, they are implicitly public, static and final. - Multiple inheritance is not supported in Java but a class can implement multiple interfaces

Uploaded by

Aman Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

1.Which of these cannot be used for a variable name in java ?

A. Identifier & keyword


B. Identifier
C. Keyword
D. None of the mentioned

Answer:-C

2.Which of these are selection statements in java


A. break
B. continue
C. for ( )
D. if ( )

Answer:-D

3.Which statement is true about Java?


A. Java is a sequence-dependent programming language
B. Java is a code dependent programming language
C. Java is a platform-dependent programming language
D. Java is a platform-independent programming language

Answer:-D

4.Which of these cannot be used for a variable name in Java?

A. identifier & keyword


B. identifier
C. keyword
D. none of the mentioned

Answer:-C

5.What is the value of g?


int g = 3;
System.out.print(++g * 8);

Answer:- 4

1|7.7MCQ
6.What is the output given code.
StringBuffer s1 = new StringBuffer("Quiz");
StringBuffer s2 = s1.reverse();
System.out.println(s2);

A. QuizziuQ
B. Quiz
C. ziuQQuiz
D. ziuQ

Answer:-D

7.Select the valid statement.

A. char[] ch = new char(5);


B. char[] ch = new char[5];
C. char[] ch = new char();
D. char[] ch = new char[];

Answer:-B

8.When is the object created with new keyword?

A. At run time
B. At compile time
C. Depends on the code
D. None

Answer:-A

9.Identify the output of the following program.


String str = “abcde”;
System.out.println(str.substring(1, 3));

A. abc
B. bc
C. bcd
D. cd

Answer:-B

2|7.7MCQ
10.Identify the return type of a method that does not return any value.
A. int
B. void
C. double
D. None

Answer:-B

11.Guess The Output


int $abc=10;
System.out.println($abc);

A. 10
B. 11
C. Error
D. 13

Answer:-A

12.How many Keywords in JAVA ?


A. 32
B. 42
C. 40
D. 53

Answer:-D

13.What is a blueprint and determines how an object will behave?


A. Object
B. Method
C. Attribute
D. Class

Answer:-D

14.What the output of the below code snippet.


class A
{
int i;
}
class Main
{

3|7.7MCQ
public static void main(String[]args)
{
A a;
System.out.println(a.i);
}
}

A. 0
B. Garbage value
C. Compilation error
D. Runtime error

Answer:-C

15.Which declaration will create compile time error?


A. double num = 8;
B. int averageGrade = 89.7;
C. boolean done = false;
D. String done = "true";

Answer:-B

16.which of the these variables allowed in java.


A. Local Variables
B. Instance Variables
C. Static Variables
D. All the above

Answer:-D

17.What the ouput of the below code snippet.


public class Sum
{
public static void main(String[]args)
{
int a=10,b=20;
System.out.println("sum is "+a+b);
}
}

A. sum is 1020
B. sum is 30

4|7.7MCQ
C. airthmetic exception.
D. None of the above

Answer:-A

18.What the ouput of the below code snippet.


public class Sum
{
public static void main(String[]args)
{
int a=10,b=20;
System.out.println("sum is "+((a+b)-(a-b)));
}
}

A. 40
B. 30
C. 20
D. 10

Answer:-A

19.What the ouput of the below code snippet.


public class Difference
{
public static void main(String[]args)
{
int a=10,b=20;
System.out.println("Difference is "+a-b);
}
}

A. -10
B. 10
C. Compile time error
D. None of the above.

Answer:-C

5|7.7MCQ
20.What the ouput of the below code snippet.
file name is sample.java
class Hai
{
public static void main(String[]args)
{
System.out.println("printed hai");
}
}
//javac sample.java
//java Hai

A. Could not find or load main class sample.


B. Could not find or load main class Hai
C. printed Hai.
D. file name class name must me same

Answer:-C

21.At the end of this fragment of code what is r1.count value?

public class Result


{
int count=10;
void display()
{
count-=5;
}
public static void main(String[]args)
{
Result r1=new Result();
Result r2=new Result();
Result r3=new Result();
r1.count=6;
r2.count=7;
r3.count=8;
System.out.println(r3.count);
}
}

A. 10
B. -5

6|7.7MCQ
C. 6
D. 7
E. 8

Answer:-E

22.Predict the output of given code.


public class Sample
{
static int a=10;
int display()
{
return a;
}
public static void main(String []args)
{
Sample s1=new Sample();
Sample s2=new Sample();
s1.a=5;
System.out.println(s2.display());
}
}
A. 10
B. 5
C. Compile time error.
D. None of the above.

Answer:-B

23.Predict the output of given code.


public class Sample
{
int a=10;
void display()
{
System.out.println(a);
}
public static void main(String []args)
{
Sample s1=new Sample();
Sample s2=new Sample();
Sample s3=new Sample();

7|7.7MCQ
s1.a=25;
System.out.println(s2.a);
}
}

A. 10
B. 25
C. 10 25
D. 25 10

Answer:-A

24.Which of the following are valid declaration for main in java.


1.public static void main(String [] args)
2.public static void main(String args[])
3.static public void main(String [] args)
4.public void static main(String [] args)

A. 1,2
B. 2,3
C. 1,2,3
D. 1,2,3,4

Answer:-C

25.What is instance variable?


A. Instance variables are static variables within a class but outside
any method
B. Instance variables are variables defined inside methods,
constructors or blocks
C. Instance variables are variables within a class but outside any
method
D. None of the mentioned

Answer:-C

26.Which of these operators is used to allocate memory for an object?


A. Malloc
B. Alloc
C. new
D. assign
Answer:-C

8|7.7MCQ
27.Is static is a keyword.
A.True
B.False

Answer:-A

28.Scope of the local variable?


A. within function
B. outside function
C. within class
D. outside class

Answer:-A

30.static variables can be accessed without objects.


A. True
B. False

Answer:-A

31.Find the output of given code.


public class Test1
{
//non-instance variable or data member
static int a;
//member fn
public static void main (String args[])
{
//local variable
int b=20;
Test1 ob=new Test1();
a=10;
System.out.println((a+b));
ob.display();
System.out.print((a+b));
}
//member fn
void display()
{
int c=30;
Test1 obj=new Test1();
a=20;

9|7.7MCQ
}
}

A. 30 30
B. 30 50
C. 30 40
D. 30 20

Answer:-C

32.Fill the bank Space.


class A
{
int a; String b; float c;
}

The above class contains________many instance variables

Answer:-3

33.Predict the output of the following program.


class A{
int a=40;//non static
public static void main(String args[])
{
System.out.println(a);
}
}
A. Compilation Error
B. 40
C. 0
D. None of the above

Answer:-A

34.what is the output of this question?


class Test1 {
static int x = 10;
public static void main(String[] args)
{
Test1 t1 = new Test1();
Test1 t2 = new Test1();

10 | 7 . 7 M C Q
t1.x = 20;
System.out.print(t1.x + " ");
System.out.println(t2.x);
}
}
A. 10 10
B. 20 20
C. 10 20
D. 20 10

Answer:-B

35.What is the output of this question?


class Test1 {
static int i = 1;
public static void main(String[] args)
{
for (int i = 1; i < 10; i++) {
i = i + 2;
System.out.print(i + " ");
}
}
}
A. 3 6 9
B. 3 6 9 27
C. Error
D. none

Answer:-A

36.What is the output of this question?


class Test1 {
static int i = 1;
public static void main(String[] args)
{
int i = 1;
for (Test1.i = 1; Test1.i < 10; Test1.i++) { i = i + 2;
System.out.print(i + " ");
}
}
}

11 | 7 . 7 M C Q
A. 1 3 9
B. 1 2 3 … 9
C. 3 5 7 9 11 13 15 17 19
D. None

Answer:-C

37.Which of the following variable is declared as static?


A. Home address of a particular Student.
B. Account number of a Customer
C. Company name of all Employees in an Organization
D. Name of a Employee

Answer:-C

38.Static variables belongs to an Object?


A. Yes
B. No

Answer:-B

39.What is the output of the following code?


public class Demo
{
static int x=10;
int y =15;
public Demo()
{ x++;
y++;
}
public static void main(String[] args)
{
Demo d1= new Demo();
Demo d2= new Demo();
Demo d3= new Demo();
System.out.println(d3.x + " , " +d3.y);
}
}
A. 13,15
B. 13,16
C. 13,18
D. 10,18

12 | 7 . 7 M C Q
Answer:-B

40.How would you declare a variable storing the tax rate?

A. int taxRate = 5.1;


B. taxRate = "5.1";
C. double taxRate = 5.1;
D. double taxRate = "5.1";

Answer:-C

41.How would you declare a variable storing a person's name?


A. string name = "Elroy";
B. name String = "Elroy";
C. String name = Elroy;
D. String name = "Elroy";

Answer:-D

42.How would you declare a variable that tells you that someone passed
a class?

A. boolean passed = 'true';


B. boolean passed = true;
C. passed = true;
D. String passed = "true";
E. Trick question - no one passes this class

Answer:-B

43.What prints out "I love Java" successfully?

A. System.out.println(I love Java);


B. Systemoutprintln("I love Java);
C. System.out.println("I love" + " Java");
D. System.out.println("I love Java")

Answer:-C Option D is incorrect because ;(semicolon missing).

13 | 7 . 7 M C Q
44.Which function is used to input int value in Java using Scanner class?

A. scanner_object.nextInteger( )
B. scanner_object.nextInt( )
C. scanner_object.nextint( )
D. scanner_object.NextInt( )

Answer:-B

45.Which function is used to input float value in Java using Scanner


class?
A. scanner_object.nextFloat( )
B. scanner_object.nextInt( )
C. scanner_object.nextfloat( )
D. scanner_object.NextFloat( )

Answer:-A

46.Which package should you import to use the Scanner class in Java?
A. java.util
B. java.io
C. java.scanner
D. java.lang
E. java.system

Answer:-A

47.What is the purpose of the Scanner class in Java?


A. To format strings.
B. To manage exceptions.
C. To read the data from input sources.
D. To perform mathematical calculations.
E. To manipulate images.

Answer:-C

48.Which method is used to read an integer input from the user using the
Scanner class?
A. readInt()
B. nextInt()
C. getInt()
D. scanInt()

14 | 7 . 7 M C Q
E. inputInt()

Answer:-B

49.What is the default delimiter used by the Scanner class to tokenize


input?
A. Whitespace
B. Comma (,)
C. Period (.)
D. Semicolon (;)
E. Tab (\t)

Answer:-A

50.Which method is used to read a string input from the user using the
Scanner class?
A. readString()
B. nextString()
C. getString()
D. scanString()
E. nextLine()

Answer:-E

51.Which method checks if the Scanner object has another token available
in its input?
A. hasNext()
B. hasToken()
C. hasNextToken()
D. hasMore()
E. hasInput()

Answer:-A

52.Which method is used to read a double input from the user using the
Scanner class?
A. readDouble()
B. nextDouble()
C. getDouble()
D. scanDouble()
E. inputDouble()
Answer:-B

15 | 7 . 7 M C Q
53.What happens if you input a non-integer value when using the nextInt()
method of Scanner class?

A. The program crashes.


B. An exception is thrown at runtime.
C. The value is rounded to the nearest integer.
D. The value is stored as a string.
E. The value is automatically converted to an integer.

Answer:-B

54.Which method is used to close a Scanner object?


A. closeScanner()
B. closeInput()
C. close()
D. shutdown()
E. terminate()

Answer:-C

55.Which method is used to read a single word without spaces from the
user using the Scanner class?
A. nextWord()
B. readWord()
C. scanWord()
D. next()
E. input()

Answer:-D

56.What will be the output of the below program?

public class Demo {


public static void main(String[] args) {
int num1 = 20;
double num2 = 0;
if (num1 >= 20)
num2 = 1.5;
if (num1 < 30)
num2 = 2;
System.out.println("Value of num2 is " + num2);
}

16 | 7 . 7 M C Q
}

A. Value of num2 is 2
B. Value of num2 is 1.5
C. Value of num2 is 2.0
D. Runtime error due to datatype conversion

Answer:-C

57.What will be the output of the below program?


public class Demo {
public static void main(String[] args) {
int num1 = 25;
int num2 = 34;
if (num1 / 3 >= num2 / 4) {
num1 = num1 + 1;

} else {
num2 = num2 + 1;
}
System.out.println(num1 + "," + num2);
}
}
A. 25,35
B. 25,34
C. 26,34
D. 26,36

Answer:-C

58.What will be the output of the below program?

public class Demo {


public static void main(String[] args) {
boolean x = true;
boolean y = false;
if (x && y) {
System.out.println(true);
} else {
System.out.println(false);
}
}

17 | 7 . 7 M C Q
}
A. true
B. false
C. true false
D. Compilation Error due to condition on wrong datatype.

Answer:-B

59.What will be the output of the below program?

public class Demo {


public static void main(String[] args) {
int a = 12 * 3 - 9 / 2;
int b = 14 * 4 + 175 / 8;
if (a++ % 2 == 0) {
if (b-- % 4 == 0) {
System.out.println("a = " + a + " b = " + b);
} else {
System.out.println("a = " + a + " b = " + b);
}
} else {
System.out.println("a = " + a + " b = " + --b);
}
}
}

A. a = 32 b = 76
B. a = 33 b = 76
C. a = 33 b = 77
D. a = 32 b = 77

Answer:-B

60.What will be the output of the below program?

public class Demo {


public static void main(String[] args) {
int num1 = 100;
int num2 = 200;
int num3 = 6;

18 | 7 . 7 M C Q
if (5 >= num3) {
if (num1 > 100 || num2 > 150) {
System.out.println("1");
}
} else if (num1 >= 100 && num2 > 150) {
System.out.println("2");
} else {
System.out.println("3");
}
}
}

A. 1
B. 2
C. 3
D. 1 3

Answer:-B

61.What should be the value of num1 and num2 to get the output as 2?

public class Demo {


public static void main(String[] args) {
int num1;
int num2;
if ((num1 / num2 == 5) && (num1 + num2) > 5) {
System.out.println("1");
} else if ((num1 - num2) >= 1 || (num1 % num2) == 0) {
System.out.println("2");
} else {
System.out.println("3");
}
}
}
A. num1 = -10, num2 = 3
B. num1 = 5, num2 = 1
C. num1 = 0, num2 = 5
D. Compile-Time Error.

Answer:-D We get compile-time error because we have not initialize local


variable num1 and num2.

19 | 7 . 7 M C Q
62.What will be the output of the below program?

public class Demo {


public static void main(String[] args) {
int a = -10;
int b = -200;
int c = 2000;
int d = 4000;
if (a * b >= d) {
if (d > c) {
if (d % c != 0) {
System.out.println(11);
} else {
System.out.println(22);
}
}
} else {
if (b / a > 0) {
if (a < b || d % c != 0) {
System.out.println(33);
} else {
System.out.println(44);
}
}
}
}
}

A. 11
B. 33
C. 44
D. 22

Answer:-C

63.What will be the output of the below program?

public class Demo {


public static void main(String[] args) {
int i = -1, j = -1;
switch (i) {

20 | 7 . 7 M C Q
case -1:
j = 1;
case 2:
j = 2;
break;
default:
j = 0;
}
System.out.println("j = " + j);
}
}
A. j = 2
B. j = 1
C. j = 0
D. Compilation Error due to absence of break in first case block.

Answer:-A

64.Choose the correct option based on the code snippet given below.

public class Demo {


public static void main(String[] args) {
float f = 12;
switch (f) { // Line 1
case 10 + 1: // Line 2
System.out.println("Twelve");
case 0: // Line 3
System.out.println("Zero");
case 12.0:
System.out.println("Decimal");
default:
System.out.println("Default");
}
}
}
A. Compilation error in Line 3 “Cannot use 0 in case”.
B. Compilation error in Line 2 “Cannot use operator in case”.
C. Compilation error in Line 1 “Cannot use float type in switch”.
D. None of the above.

Answer:-C

21 | 7 . 7 M C Q
65.What will be the output of the below program?

public class Demo {


public static void main(String[] args) {
double i;
char j = 'b';
switch (j) {
case 'a':
case 'A':
i = 7.5;
break;
case 'b':
case 'B':
i = 5.5;
break;
case 'c':
case 'C':
i = 2.5;
break;
default:
i = 0.5;
}
System.out.println(i);
}
}
A. Compilation error as each case block must have at least one
statement
B. Compilation error as variable i is not initialized while
declaration
C. 0.5
D. 5.5

Answer:-D

66.What will be the output of the below program?

public class Demo {


public static void main(String[] args) {
int k = 1;
switch (k) {
default:
System.out.println("Hello");

22 | 7 . 7 M C Q
case 1:
System.out.println("Welcome");
case 2:
System.out.println("To");
case 3:
System.out.println("Infosys");
break;
}
}
}

A. Hello
B. Welcome
C. Welcome
To
Infosys
D. Compilation error as default must be the last case in the switch
block

Answer:-C

67.What will be the output of the below program?


public class Demo {
public static void main(String[] args) {
int k = 2;
switch (k) {
case 'a':
System.out.println("Welcome");
case 2:
System.out.println("To");
case 'b':
System.out.println("Infosys");
break;
default:
System.out.println("Hello");
}
}
}
A. Welcome To Infosys Hello
B. To Infosys
C. To
D. Hello

23 | 7 . 7 M C Q
Answer:-B

68.What will be the output of the below program?

public class Demo {


public static void main(String[] args) {
int k = 1;
switch (k) {
default:
System.out.println("Have");
case 'a':
System.out.println("A");
case 'b':
System.out.println("Good Day");
}
}
}
A. Have A
B. Compilation error due to datatype mismatch
C. Have
D. Have
A
Good Day

Answer:-D

69.What will be the output of the below program?

public class Demo {


public static void main(String[] args) {
int i = 1;
int j = 0;
switch (i) {
case 1:
j = j + 2;
case 2:
++j;
break;
case 3:
j++;
default:
j = 5;

24 | 7 . 7 M C Q
break;
}
System.out.println(j);
}
}

A. 3
B. 5
C. 4
D. 2

Answer:-A

70.In Java, what is the purpose of a variable?


A. To store and manage data
B. To perform mathematical operations
C. To define classes and methods
D. To control program flow

Answer: A

71.Which of the following is a valid variable name in Java?


A. 123variable
B. _myVariable
C. break
D. my-variable

Answer: B

72.What is the data type of the variable declared with the keyword float
in Java?
A) Integer
B) Floating-point
C) Character
D) Boolean

Answer:-B

73.In Java, which keyword is used to declare a constant variable?


A) final
B) static
C) const

25 | 7 . 7 M C Q
D) var

Answer: A

74. What will happen if you try to assign a value to a final variable
in Java after it has been initialized?
A) It will result in a compilation error
B) The program will throw a runtime exception
C) The final variable will be reinitialized with the new value
D) It will work without any issues

Answer: A

75.Which of the following is not a primitive data type in Java?


A) int
B) double
C) String
D) char

Answer: C

76.What is the default value of an uninitialized integer variable in


Java?
A) 0
B) 1
C) -1
D) null

Answer: A

77.What is the scope of a local variable in Java?


A) It is accessible throughout the entire class.
B) It is accessible only within the method or block where it is
declared.
C) It is accessible anywhere in the program.
D) It is accessible only in the class but not within methods.

Answer:-B

26 | 7 . 7 M C Q
78.What is the maximum value that can be stored in a byte data type in
Java?
A) 255
B) 127
C) 32767
D) 2147483647

Answer: B

79.Which of the following data types is used to store a single Unicode


character in Java?
A) int
B) char
C) double
D) boolean

Answer: B

27 | 7 . 7 M C Q

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy