0% found this document useful (0 votes)
2 views

Practice MCQs Java - Sheet1

The document contains a series of Java programming questions along with multiple-choice answers and the correct answer for each question. It covers various topics including syntax, data types, control structures, exceptions, and object-oriented programming concepts. The questions are designed to test knowledge of Java programming and its features.

Uploaded by

Vanshika Sardana
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)
2 views

Practice MCQs Java - Sheet1

The document contains a series of Java programming questions along with multiple-choice answers and the correct answer for each question. It covers various topics including syntax, data types, control structures, exceptions, and object-oriented programming concepts. The questions are designed to test knowledge of Java programming and its features.

Uploaded by

Vanshika Sardana
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

Question Description Answer Choice 1 Answer Choice 2 Answer Choice 3 Answer Choice 4 Correct Answer Choice

public static void main(String[] args) {


int x = 5;
System.out.println(x++ + ++x);
} 11 12 13 14 2
public static void main(String[] args) {
int x = 10;
int y = x > 5 ? (x < 20 ? 15 : 25) : 5;
System.out.println(y);
} 5 10 15 25 3
public static void main(String[] args) {
int number = 2;
switch (number) {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
default:
System.out.println("Default"); Two
} Three
} Two Default Three Default 2
public static void main(String[] args) {
int arr[] = new int[5];
System.out.println(Arrays.toString(arr));
} [null, null, null, null, null] [1, 1, 1, 1, 1] [0, 0, 0, 0, 0] Compilation Error 3
public static void main(String[] args) {
int arr[][] = {{1, 2, 3}, {4, 5}};
System.out.println(arr[1].length);
} 2 3 Compilation Error 5 1
public static void main(String[] args) {
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
System.out.println(arr.length + arr[1].length);
} 4 5 6 8 3
public static void main(String[] args) {int arr[] = {1, 2, 3, 4};
System.out.println(arr[arr[1]]); 1 2 3 4 3
public static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5};
System.out.println(arr[arr.length - arr[2]]);
} 1 2 3 4 3
public static void main(String[] args) {
int arr[] = new int[3];
for (int i : arr){
i = 10;
}
System.out.println(arr[0]);
} 0 10 3 Compilation Error 1
Which keyword is used to create a package in Java? import package namespace module 2
What is the default access modifier for classes in a package if none is specified?
private public package-private protected 3
How do you access a class from a user-defined package? import package.*; then use ClassNameJust use ClassName include package.ClassName using package.ClassName 1
What is the correct way to import all classes from a package `myPackage`? import myPackage; include myPackage.*; import myPackage.*; using myPackage.*; 3
What happens if a class is declared public but placed in a file with a different
Compiles
name?
successfully Runtime error Compilation error Runs with warning 3
Which of the following allows interfaces to have method bodies (from Javastatic8 onward)?
methods private methods default methods All of the above 4
Can an interface inherit another interface? No Yes, only one Yes, multiple Only abstract classes can 3
What is the output of the `javac` command? Class file JAR file Source code None 1
Which loop executes at least once? `for` `while` `do-while` None 3
An array with
What is a jagged array in Java? unequal columns A 3D array A sparse array None 1
What is the size of a char in Java? 1 byte 4 byte 3 byte 2 byte 4
What is passed to the `main()` method in Java as arguments? Array of Strings Integer values Boolean values Array of Objects 1
Which operator is used to concatenate strings in Java? + concatenate() cat() || 1
Which of these is a checked exception? NullPointerException IOException ArithmeticException
ArrayIndexOutOfBoundsException 2
Which method gets the current time in Java 8 Date-Time API? LocalTime.now() Date.now() Clock.time() Time.now() 1
Which class is used to read characters from a file? FileReader FileWriter File InputStream 1
FileWriter writer = new FileWriter("out.txt");
How to write text using character streams? writer.write("Hello");
FileReader writer = new FileReader("out.txt");
File writer = new File("out.txt");
Files.write("out.txt"); 1
Which stream should be used to write binary data? FileWriter BufferedReader FileOutputStream FileReader 3
What is required for a class to be serializable? Implements Serializable Extends Thread Implements Closeable Extends File 1
Splits strings using
What does the `StringTokenizer` class do? Sorts strings Joins strings delimiters Deletes strings 3
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder("abc");
StringBuilder sb2 = new StringBuilder("abc");
System.out.println(sb1.equals(sb2));
} 1 TRUE FALSE 0 3
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("abcdef");
sb.delete(2, 5);
System.out.println(sb);
} ab abcdef abc abf 4
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Coding");
sb.setLength(2);
System.out.println(sb);
} Co Coding Cod error 1
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("a:b:c", ":");
System.out.println(st.nextToken() + st.nextToken());
} aa a:b abc ab 4
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
System.out.println(s1 == s2);
} TRUE FALSE Compilation error Runtime error 1
public static void main(String[] args) {
String s = "Java";
s += " DSA";
System.out.println(s);
} Java DSA Error Java DSA 4
public static void main(String[] args) {
String s1 = "abc";
String s2 = "a" + "b" + "c";
System.out.println(s1.equals(s2));
} FALSE TRUE Compilation error Runtime error 2
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("a:b:c");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken()); a
} b
} a:b:c c a error 1
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("abc");
StringBuffer sb2 = new StringBuffer("abc");
System.out.println(sb1.equals(sb2));
} 1 TRUE FALSE 0 3
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("abcdef");
sb.replace(2, 3, "wxyz");
System.out.println(sb);
} abwdef abwxyzef abwxyz abwxyzdef 4
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Java";
String s3 = new String("Java");
System.out.println(s1 == s3);
} TRUE FALSE Compilation Error Runtime Error 2
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("A,B,C", ",");
System.out.println(st.countTokens());
} 2 3 5 4 2
public class Main {
Main(int x) {}
public static void main(String[] args) {
Main t = new Main();
}
} It will compile and run successfullyCompilation Error Run-time error Calls default constructor 2
class test{
private test(){
System.out.println("Constructor called");
}
}
public class Main {
public static void main(String[] args) {
test m = new test();
}
} Compilation error Constructor Called runtine error No Output 1
class test{
void display(double a) { System.out.println("Double: " + a); }
}
public class Main {
public static void main(String[] args) {
test t = new test();
t.display(5);
}
} Compilation error Double: 5.0 runtime error no Output 2
public class Main {
private Main(){
System.out.println("Constructor Called");
}
public static void main(String[] args) {
Main m = new Main();
}
} Constructor Called Compilation error runtine error No Output 1
public static void main(String[] args) {
Integer x = new Integer(50);
Integer y = 50;
System.out.println(x == y);
} Compilation error TRUE FALSE runtime error 3
public class Main {
static void print(){
System.out.println("Print called");
}
public static void main(String[] args) {
print();
}
} runtime error compilation error no output Print called 4
public class Main {
static int x = 10;
Main(){
x += 5;
}
public static void main(String[] args) {
Main m1 = new Main();
Main m2 = new Main();
System.out.println(Main.x);
}
} 10 15 20 Error 3
class test{
void display(){
System.out.println("Display");
}
}
public class Main {
public static void main(String[] args) {
test m = null;
m.display();
}
} Display compilation error Error NullPointerException Exception 4
public class Main {
void print(){
System.out.println("Hello World");
}
public static void main(String[] args) {
print();
}
} Hello World compilation error runtime error no output 2
public static void main(String args[]) {
for (byte b = 0; b < 128; b++) {
System.out.println(b);
}
} Output will be
numbers 0 to 127 Compilation Error Infinite Loop None of these 3
What will be the result of (7 >> 2) in Java? 3 2 1 0 3
What is the output?
try { int arr[] = new int[5]; arr[5] = 100; }
catch(ArrayIndexOutOfBoundsException e) { System.out.println("Error");100} Error Compile error Runtime error 2
What is the purpose of 'finally' block? Handle runtime errors Execute code after catch Clean up resources Declare exceptions 3
What will be the output?
try { System.out.println("A"); } A
finally { System.out.println("B"); } A B B Compile error 2
Which is faster for single-threaded operations? StringBuffer StringTokenizer StringBuilder String 3
int[][] arr = new int[3] int[][] arr = new int int arr[][] = new int
Which of these declarations will cause a compilation error? []; int[] arr[] = new int[3][3]; [][3]; [3][3]; 3
Which is true about multiple catch blocks? Only one is executed All are executed They are optional Must be followed by finally 1
public static void main(String args[]) {

int a =10;
String b = "10+10";

System.out.println(a+b);

} 30 1020 10+10+10 1010+10 4


Can interfaces have variables?
interface A { int x = 5; } Yes, public static final by default No Only static Only private 1
public class Main {
static int function(){
return 10;
}
static String function(){
return "20";
}
public static void main(String[] args) {
int a = function();
System.out.println(a);
}
} compilation error runtime error 10 20 1
class parent{
final void display(){
System.out.println("parent");
}
}
class child extends parent{
void display(){
System.out.println("child");
}
}
public class Main {
public static void main(String[] args) {
child c = new child();
c.display();
}
} parent child compilation error runtime error 3
abstract class test{
int a;
}
public class Main {
public static void main(String[] args) {
test t = new test();
t.a = 10;
System.out.println(t.a);
}
} compilation error 10 runtime error 0 1
class parent{
void display(){
System.out.println("parent class");
}
}
class child extends parent{
int display(){
System.out.println("child class");
return 10;
}
}
public class Main {
public static void main(String[] args) {
child c = new child();
c.display();
}
} parent class child class compilation error runtime error 3
public class Main {
int x;
Main(){
x = 10;
}
public static void main(String[] args) {
Main m1 = new Main();
Main m2 = new Main(m1);
m1.x = 20;
System.out.println(m2.x);
}
} 20 10 30 Compilation error 4
final class test{
int a;
int b;
}
class exam extends test{
int x;
}
public class Main {
public static void main(String[] args) {
test t = new test();
t.a = 10;
t.b = 20;
t = new test();
}
} class exam extends test{
int x; t.a = 10;
which is not a valid syntax in above code } test t = new test(); t.b = 20; t = new test(); 1
class parent{
void display(){
System.out.println("parent class");
}
}
class child extends parent{
void display(){
System.out.println("child class");
}
}
public class Main {
public static void main(String[] args) {
child c = new child();
c.display();
}
} parent class child class compilation error runtime error 2
public class Main {
int x;
Main(){
x = 10;
}
public static void main(String[] args) {
Main m1 = new Main();
Main m2 = m1;
m1.x = 20;
System.out.println(m2.x);
}
} 20 10 30 Compilation error 1
class parent{
int a;
parent(int a){
this.a = a;
}
}
class test extends parent{
int b;
test(int b){
this.b = b;
}
}
public class Main {
public static void main(String[] args) {
test t = new test(10);
t.a = 20;
System.out.println(t.a);
}
} 20 10 compilation error runtime error 3
What is the output?
Random rand = new Random();
System.out.println(rand.nextInt(10)); Compile-time error A random int between 0 and 9 Always prints 10 Runtime error 2
What does the following code do?
File file = new File("data.txt");
file.createNewFile(); Creates a new file Deletes the file Reads file content Renames the file 1
What is the output of:
System.out.println(new File("test.txt").exists());
when the file doesn't exist? TRUE FALSE Error null 2
class test{
int a;
test(){
a = 5;
}
}
public class Main {
public static void main(String[] args) {
final test t = new test();
t.a = 10;
t = new test();
System.out.println(t.a);
}
} 5 10 compilation error No Output 3
abstract class A{
int a;
abstract void display();
}
class test extends A{
int b;
}
public class Main {
public static void main(String[] args) {
test t = new test();
t.a = 10;
System.out.println(t.a);
}
} 10 compilation error null runtime error 2
abstract class test{
private abstract void display();
}
public class Main {
public static void main(String[] args) {
System.out.println("Test");
}
} Test runtime error no output compilation error 4
class A{
protected void display(){
System.out.println("Parent class");
}
}
class test extends A{
void display(){
System.out.println("Child class");
}
}
public class Main {
public static void main(String[] args) {
test t = new test();
t.display();
Parent Class Child Class compilation error runtime error 3
final class test{
int b;
test(int b){
this.b = b;
}
}
public class Main {
public static void main(String[] args) {
test t = new test(10);
System.out.println(t.b);
}
} 10 20 compilation error runtime error 1
which of the following is correct statement regarding
Java
Multiple
doesn't
Inheritance
support multiple
in Java inheritance
allowed
at all on Classes allowed on abstract classes
allowed on interfaces 4
interface A {
void show();
}
class Main implements A {
public void show() {
System.out.println("Hello");
}
public static void main(String[] args) {
new Main().show();
}
} Error: Interface can't be implemented Hello No output Runtime Exception 2
Which of the following correctly implements an interface?
interface MyInterface { void doStuff(); }
class MyClass _____ MyInterface {
public void doStuff() {
System.out.println("Done");
}
} extends implements inherits uses 2
What is the output?
interface X { int A = 10; }
class Y implements X {
public static void main(String args[]) {
System.out.println(A);
}
} 0 10 compile error runtime error 2
interface A {
default void print() {
System.out.println("A");
}
}
class B implements A {
public static void main(String[] args) {
new B().print();
}
} A B Compile Error Runtime Error 1
Which of the following is correct for interface inheritance?
interface A { void show(); }
interface B extends A { void display(); } Interfaces can't extend Only one interface allowed Valid syntax Class must be abstract 3
What happens here?
interface A { void show(); }
class B implements A {} Compile error: method show() not implemented No error Runtime error Abstract method error 1
Choose the correct implementation.
interface Printable { void print(); }
class Doc implements Printable {
public void print() {
System.out.println("Printing"); } } Correct Incorrect: class must beIncorrect:
abstract interface must be extended
Incorrect syntax 1
What is the output?
interface A {
static void print() { System.out.println("Hello"); }
}
class B {
public static void main(String[] args) {
A.print();
}
} Compile error Hello Nothing Runtime error 2
interface A { void display(); }
class B implements A {
public void display() {
System.out.print("B");
}
}
class C extends B {
public void display() {
System.out.print("C");
}
public static void main(String args[]) {
new C().display();
}
} B C BC Compile Error 2
public class Test {
public static void main(String args[]) {
try { int data = 50/0; }
catch(ArithmeticException e) {
System.out.println("Exception caught");
}
}
} Exception caught Compile error 0 Runtime error 1
What will be the output?
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaab");
System.out.println(m.matches()); TRUE FALSE compile error runtime error 1
What does the following print?
LocalDate date = LocalDate.of(2023, 4, 19);
System.out.println(date.getYear()); 2022 2023 2024 Compile Error 2

You might also like