0% found this document useful (0 votes)
30 views12 pages

MOCK Exam 1

The document contains a series of multiple-choice questions related to Java programming concepts, including features of Java, output of code snippets, array declarations, variable declarations, and method functionalities. It covers topics such as exception handling, class structures, and the use of keywords in Java. Each question provides options for answers, testing knowledge of Java syntax and functionality.
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)
30 views12 pages

MOCK Exam 1

The document contains a series of multiple-choice questions related to Java programming concepts, including features of Java, output of code snippets, array declarations, variable declarations, and method functionalities. It covers topics such as exception handling, class structures, and the use of keywords in Java. Each question provides options for answers, testing knowledge of Java syntax and functionality.
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/ 12

1. Which of the following is the features of java?

a. Multithreading
b. Platform independent
c. Object Oriented programming
d. All of the above

2. What is the output of the following program?

1. public class App {


2. public static void main(String[] args) {
3. int x = x + 4;
4. System.out.println(x);
5.
6. }
7. }

a. 5
b. 4
c. Compilation error
d. X

3. What is the output of following statement?


System.out.println(3+2+”6”);
a) 65
b) 326
c) 21
d) 56

4. Choose the appropriate statement to declare and initialize an array?


a) int[] A = {1,2,3};
b) int [][] A = {1,2,3};

c) int[] intArray = new int[]{1,2,3,4};

d) Both a and c
5. Which of the following identifiers for variable declaration is invalid?

a. String name1
b. String 1_name
c. String name1_
d. String $1name

6. What will be the value of b that will be printed in the following block of code?

public class Test {


public static void main(String[] args) {
int a=20,b=10;
if(a<++b && b++<25){
b+=5;
}
System.out.println(b);
}
}

a) 17
b) 12
c) 10
d) 11

7. Which loop is used when you don’t know the how many repetition will be there?
a. While
b. For
c. Do-while
d. If-else

8. Which method of Scanner class is used to get double value from the user?
a. nextDouble()
b. nextInt()
c. nextdouble()
d. nextLine()

9. What is the output of the following program?

public class App {


public static void main(String[] args) {
int num[] ={8,9,2,12,3,4};
int n=0;
for(int i=0; i<=4; i++){
if(num[i]%2==0)
n++;
}
System.out.println(n);
}
}

a. 3
b. 5
c. 4
d. Error

10. What will be the output if day is set to 1?

switch (day) {
case 1:
case 7:
System.out.println("Weekend");
case 2:
case 3:
case 4:
case 5:
System.out.println("Weekday");
break;
default:
System.out.println("Invalid day");
}

a) Weekend

b) Weekend Weekday

c) Invalid day

d) Weekday

11. What is the return type of a method that does not return any value?

a) void

b) int

c) String

d) The return type is not specified.

12. What will be the output of the following program?

class Animal {
public String makeSound(){
return "Animal makes sound";
}
public static void main(String[] args) {
Animal animal = new Animal();
System.out.println(animal.makeSound());

}
}

a. Animal makes sound


b. Compile error as we can not print a method
c. animal.makeSound()
d. “Animal makes sound”
13. What will be the output of the following program?

class Loop {
public static void main(String[] args) {
int[] numbers = {7,8,9,12,10};
for(int number : numbers){
System.out.println(number+",");
}

}
}

a. 7 8 9 12 10
b. 7,8,9,12,10,
c. 7891210
d. Error

14. What will be the output of the following program?

1. class SelectionStatements
2. {
3. public static void main(String args[])
4. {
5. int var1 = 5;
6. int var2 = 6;
7. if ((var2 = 1) == var1)
8. System.out.print(var2);
9.
10. else
11. System.out.print(++var2);
12. }
}

a. Error
b. 2
c. 7
d. 6
15. Which of the following is not a valid declaration of an array?

A) int [ ] a = new int [7];


B) int a [ ] [ ] = new int [7] [7];
C) int [ ] [ ] a = new int [3] [ ];
D) int [ ] [ ] a = new int [ ] [3];

16. Which of the following is a valid long literal?


A) ABH8097
B) L990023
C) 904423
D) 0xnf029L

17. Which of this method of class String is used to extract single character from a String

object?

A) chatat()

B) CHARAT()

C) ChatAt()

D) charAt()

18. Strings in Java are:

a) Mutable

b) Immutable- Once created, their content cannot be changed.

c) Dependent on the garbage collector for memory management


d) All of the above

19. How do you create an empty string object in Java?

a) String str = "";

b) String str = null;

c) String str = new String("")

d) Both a and c

20. Can a class implement multiple interfaces in Java?

a) No

b) Yes

c) Only if the interfaces are in the same package

d) Only if there are no abstract methods in the interfaces

21. Which keyword is used to declare an abstract class in Java?

a) abstract

b) interface

c) define

d) class abstract

22. What is the output of the following code?

ArrayList<String> colors = new ArrayList<>();


colors.add("Red");
colors.add("Blue");
colors.add("Green");
String firstColor = colors.get(0);

System.out.println(firstColor);

a) colors
b) null
c) Red
d) An error

23. What is the correct way of importing only JTextField in java?


a. import javax.swing.*;
b. import javax.JTextField;
c. import javax.swing.JTextField;
d. import java.awt.event.JTextField;

24. What is the superclass of most Swing components?

a) Component

b) JComponent

c) JFrame

d) JPanel

25. What is the purpose of a try-catch block in Java?

a) To repeat a block of code a specific number of times

b) To handle unexpected errors or exceptions that may occur during program execution.

c) To improve the readability of code

d) To define a set of variables that can only be used within the block
26. Which is the super class of all java exceptions classes?

A) Exception
B) RuntimeException
C) Throwable
D) IOException

27. What will be the output of the following Java code?

public class Try

{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}

A) Compilation fails
B) Finally
C) The code runs with no output
D) An exception is thrown at runtime

28. What is the package of String class?


a. java.util
b. java.lang
c. javax.swing
d. java.string
29. What will be printed by this code?

public class Calculator {

public int add(int num1, int num2) {


return num1 + num2;
}

public static void main(String[] args) {


Calculator calc = new Calculator();
int sum = calc.add(5, 3);
System.out.println(sum);
}
}

a) Error
b) Calculator
c) 8
d) null

30. What is the difference between calling a static method and an instance method?

a) Static methods can be called without creating an object, while instance


methods require an object instance.
b) Static methods can only return primitive data types, while instance methods
can return any data type.
c) Static methods are used for calculations, while instance methods are used
for object manipulation.
d) There is no difference, both types of methods can be called in the same
way.
31. What is the purpose of this keyword inside a constructor?

public class Person {

private String name;

public Person(String name) {

this.name = name; // Using this keyword

a) To refer to the current object being created


b) To call another constructor from the same class.
c) To access static members of the class.
d) It is not mandatory to use this inside a constructor.

32. What is the super keyword used for in inheritance?

public class Bird extends Animal {

public void makeSound() {

super.makeSound(); // Calling the parent class method

System.out.println("Chirp!");

a) To define a method that overrides a parent class method


b) To access private members of the parent class
c) To call a constructor from the parent class
d) To redefine the behavior of a method inherited from the parent class

You might also like