0% found this document useful (0 votes)
13 views22 pages

Java Programming Concepts and Exercises

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

Java Programming Concepts and Exercises

Java practice programs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name of the Dept. Computer Engineering Name of the Subject.

Java
Programming

JAVA PROGRAMMING:

 BASICS OF JAVA AND OVERLOADING

 CONCEPTS OF INHERITANCE

 OVERRIDING

 INTERFACES AND PACKAGES

 I/O STREAMS AND COLLECTIONS

 EXCEPTION HANDLING AND

 MULTITHREADED PROGRAMMING

 APPLETS

 AWT AND EVENT HANDLING.

ADITYA POLYTECHNIC COLLEGES Page | 1


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming

Array A) 15 B) 19 C) 26 D) 34
Consider the following Java code snippet: Constructor
public class ArrayExample { Consider the following Java code snipett:
public static void main(String[] args) { public class Car {
int[] numbers = {5, 10, 15, 20, 25}; private String make;
[Link](numbers[2]); private String model;
[Link]([Link]); private int year;
[Link](numbers[[Link] - 1]); // Constructor 1
[Link](numbers[3] + numbers[4]); public Car() {
[Link](numbers[[Link]]); make = "Unknown";
} model = "Unknown";
} year = 0;
Select the correct answers for the following }
questions: // Constructor 2
1. What is the output of numbers[2]? public Car(String make, String model, int year) {
A) 5 B) 10 C) 15 D) 20 [Link] = make;
2. What is the output of [Link]? [Link] = model;
A) 4 B) 5 C) 10 D) 25 [Link] = year;
3. What is the output of numbers[[Link] - }
1]? // Constructor 3
A) 5 B) 10 C) 15 D) 25 public Car(String make, int year) {
4. What is the output of numbers[3] + numbers[4]? [Link] = make;
A) 25 B) 35 C) 40 D) 45 [Link] = "Unknown";
Consider the following Java code snippet: [Link] = year;
public class ArrayOperations { }
public static void main(String[] args) { public void displayInfo() {
int[] numbers = {3, 7, 2, 9, 5}; [Link]("Make: " + make + ", Model:
[Link](numbers[2]); "+
[Link]([Link]); model + ", Year: " + year);
[Link](numbers[[Link] - 1]); }
[Link](numbers[1] + numbers[3]); // public static void main(String[] args) {
Output: ? Car car1 = new Car();
int sum = 0; Car car2 = new Car("Toyota", "Camry", 2020);
for (int i = 0; i < [Link]; i++) { Car car3 = new Car("Honda", 2018);
sum += numbers[i]; // Question 1
} [Link](); // Output: ?
[Link](sum); // Question 2
} [Link](); // Output: ?
} // Question 3
Select the correct answers for the following [Link](); // Output: ?
questions: }
5. What is the output of numbers[2]? }
A) 2 B) 3 C) 7 D) 9 Select the correct answers for the following
6. What is the output of [Link]? questions:
A) 3 B) 4 C) 5 D) 6 [Link] is the output of [Link]()?
7. What is the output of numbers[[Link] - A) Make: Unknown, Model: Unknown, Year: 0
1]? B) Make: Toyota, Model: Camry, Year: 2020
A) 3 B) 5 C) 7 D) 9 C) Make: Honda, Model: Unknown, Year: 2018
8. What is the output of numbers[1] + numbers[3]? D) Compilation Error
A) 10 B) 12 C) 14 D) 16 [Link] is the output of [Link]()?
9. What is the output of the sum of all elements in A) Make: Unknown, Model: Unknown, Year: 0
the array? B) Make: Toyota, Model: Camry, Year: 2020

ADITYA POLYTECHNIC COLLEGES Page | 2


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
C) Make: Honda, Model: Unknown, Year: 2018 B) Title: Java Programming, Author: John Smith,
D) Compilation Error Pages: 500
[Link] is the output of [Link]()? C) Title: Python Basics, Author: Jane Doe, Pages:
A) Make: Unknown, Model: Unknown, Year: 0 0
B) Make: Toyota, Model: Camry, Year: 2020 D) Compilation Error
C) Make: Honda, Model: Unknown, Year: 2018 [Link] is the output of [Link]()?
D) Compilation Error A) Title: Unknown, Author: Unknown, Pages: 0
Consider the following Java code snippet: B) Title: Java Programming, Author: John Smith,
public class Book { Pages: 500
private String title; C) Title: Python Basics, Author: Jane Doe, Pages:
private String author; 0
private int pages; D) Compilation Error
// Constructor 1 [Link] is the output of [Link]()?
public Book() { A) Title: Unknown, Author: Unknown, Pages: 0
title = "Unknown"; B) Title: Java Programming, Author: John Smith,
author = "Unknown"; Pages: 500
pages = 0; C) Title: Python Basics, Author: Jane Doe, Pages:
} 0
// Constructor 2 D) Compilation Error
public Book(String title, String author, int pages) Method overloading
{ [Link] of the following statements about method
[Link] = title; overloading in Java is true?
[Link] = author; A) Method overloading allows methods with the
[Link] = pages; same name but different return types.
} B) Method overloading occurs when a subclass
// Constructor 3 provides a specific implementation of a method
public Book(String title, String author) { that is already defined in its superclass.
[Link] = title; C) Method overloading allows methods with the
[Link] = author; same name and parameters but different access
[Link] = 0; modifiers.
} D) Method overloading is determined at runtime
public void displayInfo() { based on the actual object being referred to by the
[Link]("Title: " + title + ", Author: " reference variable.
+ author + ", Pages: " + pages); [Link] of the following method signatures
} demonstrate method overloading?
public static void main(String[] args) { A) public void calculate(int x, int y)
Book book1 = new Book(); B) public int calculate(int x, int y)
Book book2 = new Book("Java Programming", C) public void calculate(double x, double y)
"John Smith", 500); D) Both A and C
Book book3 = new Book("Python Basics", "Jane Consider the following Java code:
Doe"); public class Example {
// Question 1 public void display(int x) {
[Link](); // Output: ? [Link]("Displaying integer: " + x);
// Question 2 }
[Link](); // Output: ? public void display(double x) {
// Question 3 [Link]("Displaying double: " + x);
[Link](); // Output: ? }
} public static void main(String[] args) {
} Example obj = new Example();
Select the correct answers for the following [Link](5);
questions: [Link](5.5);
[Link] is the output of [Link]()? }
A) Title: Unknown, Author: Unknown, Pages: 0 }

ADITYA POLYTECHNIC COLLEGES Page | 3


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
[Link] is the output of the main method? D) public Example(int x, int y)
A) Displaying integer: 5 Static keyword
Displaying double: 5.5 [Link] of the following statements about the
B) Displaying double: 5 static keyword in Java is true?
Displaying double: 5.5 A) Static variables are initialized only when an
C) Displaying integer: 5 object of the class is created.
Displaying integer: 5.5 B) Static methods can access non-static variables
D) Compilation error and methods directly.
[Link] of the following method signatures would C) Static variables belong to the class rather than
NOT result in method overloading? to instances of the class.
A) public void calculate(int x, double y) D) Static methods can be overridden in
B) public int calculate(int x, double y) subclasses.
C) public void calculate(double x, int y) Consider the following Java code snippet:
D) public void calculate(int x, int y) public class MyClass {
Constructor Overloading: private static int count = 0;
[Link] of the following statements about public MyClass() {
constructor overloading in Java is true? count++;
A) Constructor overloading allows constructors }
with the same name but different return types. public static int getCount() {
B) Constructor overloading occurs when a return count;
subclass provides a specific implementation of a }
constructor that is already defined in its public static void main(String[] args) {
superclass. MyClass obj1 = new MyClass();
C) Constructor overloading allows constructors MyClass obj2 = new MyClass();
with the same name and parameters but different [Link]([Link]());
access modifiers. }
D) Constructor overloading is determined at }
runtime based on the actual object being referred [Link] is the output of the main method?
to by the reference variable. A) 0 B) 1 C) 2 D) Compilation Error
Consider the following Java code: [Link] of the following statements regarding
public class Example { static methods is correct?
private int value; A) Static methods can be overridden in
public Example() { subclasses.
value = 0; B) Static methods can access instance variables
} directly.
public Example(int x) { C) Static methods are always faster than non-
value = x; static methods.
} D) Static methods cannot be called using object
public static void main(String[] args) { references.
Example obj1 = new Example(); Consider the following Java code snippet:
Example obj2 = new Example(10); public class MyClass {
[Link]([Link]); public static void display() {
[Link]([Link]); [Link]("Hello, World!");
} }
} public static void main(String[] args) {
[Link] is the output of the main method? MyClass obj = null;
A) 0 10 B) 0 0 [Link]();
C) Compilation error D) Runtime error }
[Link] of the following constructor signatures }
would NOT result in constructor overloading? [Link] is the output of the main method?
A) public Example(int x, double y) A) Hello, World!
B) public Example(int x, double y, String z) B) NullPointerException
C) public Example(double x, int y) C) Compilation Error

ADITYA POLYTECHNIC COLLEGES Page | 4


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
D) No output, program exits without printing [Link] of the following statements about static
anything methods in Java is true?
[Link] of the following statements about static A) Static methods can access instance variables
variables in Java is true? directly.
A) Static variables are initialized when an object B) Static methods cannot be called using class
of the class is created. names.
B) Static variables are always initialized to C) Static methods can be overridden in
default values. subclasses.
C) Static variables can be accessed using object D) Static methods cannot access non-static
references. methods directly.
D) Static variables are shared among all instances Consider the following Java code snippet:
of the class. public class MyClass {
Consider the following Java code snippet: private static int count = 0;
public class MyClass { public MyClass() {
private static int count = 0; count++;
public MyClass() { }
count++; public static void displayCount() {
} [Link]("Count: " + count);
public static int getCount() { }
return count; public static void main(String[] args) {
} MyClass obj1 = new MyClass();
public static void main(String[] args) { MyClass obj2 = new MyClass();
MyClass obj1 = new MyClass(); [Link]();
MyClass obj2 = new MyClass(); }
[Link]([Link]()); }
} [Link] is the output of the main method?
} A) Count: 0 B) Count: 1
[Link] is the output of the main method? C) Count: 2 D) Compilation Error
A) 0 B) 1 C) 2 D) Compilation Error [Link] access specifier restricts access to the
[Link] of the following statements regarding class only within the same package?
static blocks in Java is correct? A) private B) protected
A) Static blocks are executed when an instance of C) public D) default (package-private)
the class is created. [Link] of the following statements regarding
B) Static blocks can access instance variables static variables and instance variables is true?
directly. A) Static variables are initialized when an object
C) Static blocks are executed before the execution is created, while instance variables are initialized
of the main method. when the class is loaded.
D) Static blocks can have return statements. B) Static variables belong to instances of the
Consider the following Java code snippet: class, while instance variables belong to the class
public class MyClass { itself.
static { C) Static variables are shared among all instances
[Link]("Static block"); of the class, while each instance of the class has
} its own copy of instance variables.
public static void main(String[] args) { D) Static variables can be accessed using object
[Link]("Main method"); references, while instance variables can only be
} accessed using class names.
} Consider the following Java code snippet:
[Link] is the output when the main method is public class MyClass {
executed? private int num;
A) Static blockMain method private static int count = 0;
B) Main methodStatic block public MyClass() {
C) Static block only num = ++count;
D) Main method only }

ADITYA POLYTECHNIC COLLEGES Page | 5


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
public static void main(String[] args) { extended.
MyClass obj1 = new MyClass(); C) Final classes cannot have any methods.
MyClass obj2 = new MyClass(); D) Final classes cannot have any fields.
[Link]([Link]); [Link] of the following statements about final
[Link]([Link]); variables in Java is true?
} A) A final variable can be assigned a value only
} once.
[Link] is the output of the main method? B) A final variable can be assigned a value
A) 1 2 B) 2 3 multiple times within the same method.
C) Compilation Error D) 0 0 C) A final variable must be initialized at the time
Final keyword of declaration and cannot be assigned a value
[Link] of the following statements about the final later.
keyword in Java is true? D) A final variable can only be declared within a
A) A final variable can be reassigned multiple method and cannot be declared as a class-level
times after initialization. variable.
B) A final method can be overridden by a Consider the following Java code:
subclass. public class Example {
C) A final class can be extended by another class. public final void display() {
D) A final variable must be initialized at the time [Link]("Displaying...");
of declaration or within the constructor }
[Link] of the following statements about final }
methods in Java is correct? [Link] is the significance of the final keyword in
A) Final methods can be overridden in the display() method?
subclasses. A) It indicates that the method cannot be called
B) Final methods can be inherited by subclasses from outside the class.
but cannot be overridden. B) It prevents any subclass from overriding the
C) Final methods cannot be inherited by method.
subclasses. C) It ensures that the method implementation
D) Final methods can only be called within the cannot be changed.
class where they are defined. D) It specifies that the method must be called
Consider the following Java code: before any other methods in the class.
public class Example { [Link] of the following statements about final
private final int x; parameters in Java is correct?
public Example() { A) Final parameters can be modified within the
x = 10; method.
} B) Final parameters must be initialized at the time
public void display() { of declaration.
[Link]("Value of x: " + x); C) Final parameters cannot be accessed within the
} method body.
public static void main(String[] args) { D) Final parameters ensure that the method
Example obj = new Example(); cannot change the reference to the object passed
[Link](); as an argument.
} Consider the following Java code:
} public class Example {
[Link] is the output of the main method? public final void display(final int x) {
A) Value of x: 0 [Link]("Value of x: " + x);
B) Value of x: 10 }
C) Compilation error }
D) Runtime error [Link] is the significance of the final keyword in
[Link] of the following statements about final the parameter x of the display() method?
classes in Java is correct? A) It ensures that the value of x cannot be
A) Final classes cannot be instantiated. modified within the method.
B) Final classes can be subclassed but cannot be B) It indicates that the method cannot be called

ADITYA POLYTECHNIC COLLEGES Page | 6


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
with different arguments. C) It indicates that the x variable must be
C) It prevents the method from accessing the initialized before use.
value of x. D) It ensures that the x variable is initialized only
D) It specifies that the value of x must be within the constructor of the Example class.
initialized before calling the method. String Class
Consider the following Java code: [Link] of the following methods in the String
public class Example { class is used to compare two strings
public static final double PI = 3.14159; lexicographically?
public void display() { A) compare() B) compareTo()
[Link]("Value of PI: " + PI); C) compareToString() D) equals()
} [Link] will be the output of the following code
public static void main(String[] args) { snippet?
Example obj = new Example(); String str1 = "hello";
[Link](); String str2 = new String("hello");
} [Link](str1 == str2);
} A) true
[Link] is the significance of the final keyword in B) false
the declaration of PI? C) It will throw a compilation error.
A) It ensures that the value of PI cannot be D) It will throw a runtime error.
modified once initialized. [Link] method in the String class is used to
B) It prevents the PI variable from being accessed concatenate two strings?
outside the Example class. A) concat() B) append()
C) It indicates that the PI variable is a constant C) join() D) merge()
and must be initialized before use. [Link] will be the result of the following code
D) It ensures that the PI variable is initialized snippet?
only within the constructor of the Example class String str = "Hello World";
[Link] of the following statements about final [Link]([Link](6));
methods in Java is false? A) World B) Hello
A) A final method can be overloaded in the same C) ld D) rld
class. [Link] of the following methods in the String
B) A final method can be inherited but cannot be class is used to convert all the characters in the
overridden in a subclass. string to lowercase?
C) A final method can be marked as private or A) toLower() B) toLowerCase()
static. C) lowerCase() D) convertLowerCase()
D) A final method can be declared in an abstract [Link] does the trim() method do in the String
class. class?
Consider the following Java code: A) Removes all the leading and trailing spaces
public class Example { from a string.
public final int x; B) Trims the length of the string to a specified
public Example(int x) { length.
this.x = x; C) Trims all the characters from the string except
} for alphabets.
public static void main(String[] args) { D) None of the above.
Example obj = new Example(10); [Link] of the following methods in the String
[Link]("Value of x: " + obj.x); class is used to find the index of the first
} occurrence of a specified character or substring
} within the string?
[Link] is the significance of the final keyword in A) indexOf() B) find()
the declaration of the x variable? C) search() D) contains()
A) It ensures that the x variable cannot be [Link] will be the output of the following code
accessed outside the Example class. snippet?
B) It ensures that the x variable cannot be String str = "Hello World";
modified after object creation. [Link]([Link]('o', 'a'));

ADITYA POLYTECHNIC COLLEGES Page | 7


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
A) Hello World B) Hella Warld A obj = new B();
C) Hella World D) Hella Warlad [Link]();
[Link] of the following methods in the String }
class is used to split a string into an array of }
substrings based on a delimiter? A) A B) B
A) split() B) divide() C) Compilation Error D) Runtime Error
C) separate() D) break() [Link] keyword in Java is used to prevent method
[Link] does the charAt(int index) method in the overriding?
String class return? A) final B) static
A) The character at the specified index in the C) abstract D) private
string. [Link] type of inheritance in Java allows a class
B) The index of the specified character in the to inherit from multiple interfaces?
string. A) Single Inheritance
C) The substring starting at the specified index. B) Multiple Inheritance
D) The ASCII value of the character at the C) Multilevel Inheritance
specified index. D) Hierarchical Inheritance
Inheritance all types [Link] is the output of the following Java code?
[Link] is the output of the following Java code? class A {
class A { void show() {
int x = 5; [Link]("A");
} }
class B extends A { }
int x = 10; class B extends A {
} void show() {
public class Main { [Link]();
public static void main(String[] args) { [Link]("B");
A obj = new B(); }
[Link](obj.x); }
} public class Main {
} public static void main(String[] args) {
A) 5 B) 10 B obj = new B();
C) Compilation Error D) Runtime Error [Link]();
[Link] of the following is true about Java }
inheritance? }
A) Java supports multiple inheritance of classes A) A B B) B A
B) Java supports multiple inheritance of C) Compilation Error D) Runtime Error
interfaces [Link] of the following is true about constructors
C) Java does not support inheritance at all in Java and inheritance?
D) Java supports multiple inheritance of both A) Constructors cannot be inherited
classes and interfaces B) Constructors can be inherited but cannot be
[Link] is the output of the following Java code? overridden
class A { C) Constructors can be inherited and overridden
void display() { D) Constructors are not related to inheritance
[Link]("A"); [Link] is the output of the following Java code?
} class A {
} A() {
class B extends A { [Link]("A's constructor");
void display() { }
[Link]("B"); }
} class B extends A {
} B() {
public class Main { [Link]("B's constructor");
public static void main(String[] args) { }

ADITYA POLYTECHNIC COLLEGES Page | 8


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
} }
public class Main { }
public static void main(String[] args) { A) A B B) B A
B obj = new B(); C) Compilation Error D) Runtime Error
} [Link] keyword in Java is used to implement
} method overloading?
A) A's constructor A) override B) overload
B's constructor C) overridable D) None of the above
B) B's constructor [Link] is the output of the following Java code?
A's constructor class A {
C) Compilation Error void show() {
D) Runtime Error [Link]("A");
[Link] of the following statements regarding }
abstract classes in Java is true? }
A) Abstract classes cannot have constructors class B extends A {
B) Abstract classes cannot have abstract methods void display() {
C) Abstract classes cannot be instantiated [Link]("B");
D) Abstract classes cannot be inherited }
[Link] is the output of the following Java code? }
interface A { public class Main {
default void show() { public static void main(String[] args) {
[Link]("A"); A obj = new B();
} [Link]();
} }
class B implements A { }
public void show() { A) A B) B
[Link]("B"); C) Compilation Error D) Runtime Error
} [Link] is the output of the following Java code?
} class A {
public class Main { void show() {
public static void main(String[] args) { [Link]("A");
A obj = new B(); }
[Link](); }
} class B extends A {
} void display() {
A) A B) B [Link]("B");
C) Compilation Error D) Runtime Error }
[Link] is the output of the following Java code? }
class A { public class Main {
void display() { public static void main(String[] args) {
[Link]("A"); A obj = new B();
} [Link]();
} }
class B extends A { }
void display() { A) A B) B
[Link](); C) Compilation Error D) Runtime Error
[Link]("B"); Super Keyword
} [Link] is the purpose of the "super" keyword in
} Java?
public class Main { A) To access variables and methods of the current
public static void main(String[] args) { class.
A obj = new B(); B) To invoke the constructor of the superclass.
[Link](); C) To declare a superclass for a class.

ADITYA POLYTECHNIC COLLEGES Page | 9


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
D) To access static variables and methods of the "super" keyword is false?
superclass. A) It can be used to access the constructor of the
[Link] of the following statements is true superclass from within a subclass constructor.
regarding the "super" keyword? B) It can be used to access overridden methods of
A) It can be used only within the constructor of a the superclass.
subclass. C) It can be used to access static members of the
B) It can be used to access the private members superclass.
of the superclass. D) It can be used to access the private members
C) It can be used to access the superclass of the superclass.
constructor and methods. [Link] the following code snippet:
D) It can be used to override methods of the class Vehicle {
superclass. int maxSpeed = 100;
[Link] the following code snippet: }
class Animal { class Car extends Vehicle {
String color = "White"; int maxSpeed = 200;
} void display() {
class Dog extends Animal { [Link]("Max speed of Car: " +
String color = "Black"; maxSpeeD);
void display() { [Link]("Max speed of Vehicle: " +
[Link]([Link]); [Link]);
} }
} }
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Dog dog = new Dog(); Car car = new Car();
[Link](); [Link]();
} }
} }
What will be the output of the above code? What will be the output of the above code?
A) Black A) Max speed of Car: 200, Max speed of Vehicle:
B) White 200
C) It will throw a compilation error B) Max speed of Car: 200, Max speed of Vehicle:
D) It will throw a runtime error 100
[Link] Java, which of the following statements is C) Max speed of Car: 100, Max speed of Vehicle:
correct about the "super" keyword? 200
A) It can be used to access the instance variables D) Max speed of Car: 100, Max speed of Vehicle:
of the superclass. 100
B) It can be used to access the static members of [Link] Java, can the "super" keyword be used in a
the superclass. static method?
C) It can be used to call the constructor of the A) Yes, it can be used to call static members of
superclass. the superclass.
D) It can be used to access the overridden B) No, it cannot be used in a static method.
methods of the superclass. C) Yes, it can be used to access the constructor of
[Link] happens if you do not explicitly call the the superclass.
constructor of the superclass using the "super" D) Yes, it can be used to access overridden
keyword in a subclass constructor? methods of the superclass.
A) The subclass constructor will automatically [Link] statement best describes the behavior of
call the superclass default constructor. the "super" keyword in Java?
B) It will result in a compilation error. A) It always refers to the immediate superclass of
C) The subclass constructor will not be able to the class in which it appears.
access the superclass members. B) It refers to the superclass of the superclass of
D) It will result in a runtime error. the class in which it appears.
[Link] of the following statements regarding the C) It refers to the subclass of the class in which it

ADITYA POLYTECHNIC COLLEGES Page | 10


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
appears. C) To define a contract for implementing classes
D) It refers to the current class itself. D) To enforce encapsulation
superclass of the class in which it appears. [Link] is the result of compiling and running the
[Link] happens if the superclass constructor being following code?
invoked using the "super" keyword is not interface MyInterface {
accessible from the subclass? void display();
A) It will result in a compilation error. }
B) It will automatically invoke the default class MyClass implements MyInterface {
constructor of the superclass. public void display() {
C) It will result in a runtime error. [Link]("Display method");
D) It will automatically invoke the parameterless }
constructor of the superclass. public static void main(String[] args) {
Interface MyInterface obj = new MyClass();
[Link] is the correct way to define an interface in [Link]();
Java? }
A) interface MyInterface { } }
B) class MyInterface { } A) Compilation error due to missing public
C) abstract interface MyInterface { } access modifier in display() method of MyClass
D) public interface MyInterface { } B) Compilation error due to missing public access
[Link] of the following is true about interfaces in modifier in MyInterface.
Java? C) Compilation error due to attempting to create
A) Interfaces can contain instance variables. an instance of an interface.
B) Interfaces can extend multiple classes. D) "Display method" will be printed.
C) Interfaces can contain constructor definitions. [Link] of the following is NOT a valid interface
D) Interfaces can contain method signatures. method modifier?
[Link] keyword is used to implement an interface A) abstract B) final
in a class? C) default D) static
A) extends B) implements [Link] an interface have a static method in Java?
C) implementsIn D) inherits A) Yes B) No
[Link] happens if a class implements multiple [Link] the following interface:
interfaces with the same method signature? public interface MyInterface {
A) It will result in a compilation error. void method1();
B) The class must override the method with the default void method2()
same signature. { [Link]("Default method");
C) The class can choose which method to use. }
D) The method from the first interface will be }
used by default. Which of the following statements about the
[Link] of the following statements is true about interface is correct?
interface variables in Java? A) All classes that implement this interface must
A) Interface variables can be marked as private. provide an implementation for method1().
B) Interface variables can have default values. B) Classes implementing this interface can
C) Interface variables cannot be accessed outside choose to override method2().
the interface. C) Interfaces cannot contain default methods.
D) Interface variables can be static. D) The interface cannot be instantiated.
[Link] keyword is used to access an interface [Link] of the following statements about
method implemented by a class? interface inheritance is true?
A) this B) super A) An interface cannot inherit from another
C) extends D) implements interface.
[Link] an interface extend another interface in Java? B) An interface can inherit from multiple
A) Yes B) No interfaces.
[Link] is the purpose of using interfaces in Java? C) An interface can inherit from a class.
A) To achieve multiple inheritance D) An interface cannot inherit from a class.
B) To provide a way for defining constants [Link] of the following is the correct way to

ADITYA POLYTECHNIC COLLEGES Page | 11


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
declare a constant in an interface? [Link] of the following is true about Java
A) public static final int CONSTANT = 10; package naming conventions?
B) constant int CONSTANT = 10; A) Package names should be all uppercase
C) final int CONSTANT = 10; B) Package names should be unique within a
D) static final int CONSTANT = 10; project
[Link] a class implements an interface but does not C) Package names can contain spaces
provide implementations for all the interface D) Package names cannot contain numbers
methods, the class must be declared as: [Link] happens if you attempt to import a class
A) abstract B) final C) public D) static that does not exist in the specified package?
[Link] of the following is NOT a valid return A) The compiler throws an error
type of an interface method? B) The import statement is ignored
A) void B) int C) String D) Object C) The JVM throws an error at runtime
Package(importing) D) The class is automatically generated
[Link] does the import keyword in Java do? [Link] keyword is used to access a class from a
A) It includes a package in the current file package without importing it?
B) It includes a class in the current file A) import B) package
C) It imports all classes from a package into the C) import static D) package static
current file [Link] the following import statement:
D) It imports a specific class from a package into import packageName.*;
the current file Which of the following is true?
[Link] of the following statements is true A) It imports all classes in the packageName
regarding Java package importing? package and its subpackages
A) Only one package can be imported in a single B) It imports only classes directly within the
Java file packageName package
B) Packages are automatically imported if they C) It imports all classes in the packageName
are in the same directory package but not its subpackages
C) All classes within a package are imported D) It imports all classes in the packageName
using a single import statement package excluding the specified class
D) Classes within the same package do not need [Link] Java, can you import a class from the default
to be imported package into a named package?
[Link] the following import statement: A) Yes
import [Link].*; B) No
What does it import? C) Only if the class is public
A) All classes in the [Link] package D) Only if the package is public
B) All classes in the java package [Link] of the following is true about static
C) All classes in the current directory imports in Java?
D) All classes in the util package A) Static imports are used to import static
[Link] of the following is a correct way to methods only
import a specific class ClassName from a B) Static imports allow importing all classes
package packageName? within a package
A) import [Link]; C) Static imports allow accessing static
B) import packageName.*; members of a class directly without specifying
C) import [Link]; the class name
D) import ClassName from packageName; D) Static imports are used to import non-static
[Link] Java, if two classes with the same name exist members of a class
in different packages and both are imported into [Link] Java, what is the purpose of using the .*
the same file, how can you refer to them? wildcard in an import statement?
A) By using their fully qualified names A) It imports all classes and interfaces within a
B) By renaming one of the classes during package
import B) It imports all classes and interfaces from the
C) By using wildcard import to resolve conflicts [Link] package
D) By organizing classes into different C) It imports all classes and interfaces from the
directories current directory

ADITYA POLYTECHNIC COLLEGES Page | 12


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
D) It imports all classes and interfaces from the B) It imports all classes from a package
default package statically
[Link] of the following import statements is C) It allows importing non-static members of a
incorrect in Java? class
A) import [Link].*; D) It specifies that the imported class should be
B) import [Link]; statically initialized
C) import [Link]; Access specifiers
D) import [Link]; [Link] of the following statements regarding
[Link] the following code: access specifiers in Java is true?
import [Link].*; A) The "protected" access specifier allows
import [Link].*; access only within the same class.
public class Test { B) The "private" access specifier allows access
public static void main(String[] args) { from any class within the same package.
List list = new ArrayList(); C) The "public" access specifier allows access
Connection conn = from any class in any package.
[Link]("jdbc:mysql://loc D) The "default" access specifier allows access
alhost/test", "user", "password"); from any subclass regardless of the package.
} [Link] Java, can a subclass in a different package
} access a superclass's protected member variable
Which import statement is unnecessary? directly?
A) import [Link].*; A) Yes B) No
B) import [Link].*; [Link] the following Java code:
C) Both are necessary package pack1;
D) Both are unnecessary public class ClassA {
[Link] of the following statements about protected int num = 10;
package-private access in Java is true? }
A) Package-private access allows access only package pack2;
within the same class import [Link];
B) Package-private access allows access within public class ClassB extends ClassA {
the same package and its subpackages public void display() {
C) Package-private access allows access within [Link](num);
the same package only }
Package-private access allows access from any }
package Which of the following statements is correct
[Link] is the significance of the default package regarding the code?
in Java? A) It will compile without any errors.
A) It allows access to classes from any package B) It will compile but throw a runtime error.
B) It restricts access to classes from any C) It will not compile due to an access specifier
package conflict.
C) It automatically imports all classes from the D) It will not compile due to an import error.
standard Java library [Link] access specifier allows access only
D) It does not exist, all classes must belong to a within the same class and the same package?
named package A) public B) protected
[Link] Java, can you have multiple import C) private D) default (package-private)
statements for the same package? [Link] Java, which access specifier cannot be
A) Yes applied to top-level classes?
B) No, it will result in a compilation error A) private B) protected
C) Yes, but only if the classes imported are C) public D) default (package-private)
different [Link] Java, if a member variable is declared
D) No, it is not allowed by the Java syntax without any access specifier, what is its
[Link] is the purpose of the static keyword in a accessibility?
static import statement in Java? A) It is accessible only within its class.
A) It allows importing static members of a class B) It is accessible within its class and its

ADITYA POLYTECHNIC COLLEGES Page | 13


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
subclasses. C) It enables the class to implement custom
C) It is accessible within its package. serialization logic.
D) It is accessible globally. D) It permits the class to be used as a key in
[Link] access specifier is used for methods and hash-based data structures.
variables that should not be accessible by code [Link] would you use ObjectOutputStream in
outside the class to which they belong? Java?
A) private B) protected A) To write primitive data types directly to a
C) public D) default (package-private) file.
[Link] access specifier provides the widest level B) To serialize objects and write them to an
of accessibility, allowing classes and members output stream.
to be accessed from any other class? C) To read objects from an input stream.
A) private B) protected D) To buffer input data for efficient reading.
C) public D) default (package-private) [Link] of the following statements regarding
[Link] access specifier can be applied to top- BufferedReader and Scanner classes in Java is
level classes? true?
A) private B) protected A) BufferedReader is more efficient for reading
C) public D) default (package-private) input from the console than Scanner.
[Link] access specifier is the default if no other B) Scanner is more efficient for reading large
specifier is explicitly provided? files compared to BufferedReader.
A) private B) protected C) BufferedReader is synchronized, whereas
C) public D) default (package-private) Scanner is not.
I/o Streams D) Scanner is suitable for reading formatted
[Link] is the key difference between input, while BufferedReader is suitable for
FileInputStream and DataInputStream in Java? reading raw input.
A) FileInputStream is used for reading [Link] is the purpose of the
character-based data, whereas DataInputStream ByteArrayOutputStream class in Java?
is used for reading binary data. A) To read bytes from an input stream and write
B) FileInputStream is a byte stream class, them to a file.
whereas DataInputStream is a character stream B) To provide a buffer for writing bytes to an
class. output stream.
C) FileInputStream allows reading primitive C) To read bytes from a file and convert them to
data types directly from a file, whereas characters.
DataInputStream provides methods for reading D) To compress data before writing it to an
Java primitive data types. output stream.
D) FileInputStream is used for reading data [Link] of the following statements about
from a file, whereas DataInputStream is used ObjectInputStream in Java is incorrect?
for reading data from an input stream. A) ObjectInputStream reads serialized objects
[Link] is the purpose of the mark() and reset() from an InputStream.
methods in Java I/O Streams? B) ObjectInputStream can only read objects
A) To set a bookmark at the current position in written by ObjectOutputStream.
the stream and later return to it using reset(). C) ObjectInputStream can read primitive data
B) To mark the beginning and end of a data types directly from an input stream.
block for efficient parsing. D) ObjectInputStream can deserialize objects
C) To synchronize multiple input streams for with readObject() method.
concurrent access. [Link] is the purpose of Java I/O Streams?
D) To indicate the start and end of a transaction A) To provide communication between two
in file operations. different threads
[Link] Java, what does the Serializable interface B) To perform input and output operations with
allow a class to do? various data sources
A) It allows the class to be written to an output C) To optimize memory usage in Java programs
stream and read back from an input stream. D) To facilitate inter-process communication in
B) It allows the class to be serialized and Java applications
deserialized using custom binary encoding. [Link] of the following classes is used for

ADITYA POLYTECHNIC COLLEGES Page | 14


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
character-based input in Java? [Link] is the purpose of the BufferedInputStream
A) InputStream B) Reader class in Java?
C) Writer D) OutputStream A) To read primitive data types from a binary
[Link] Java, which stream class is used to read stream
primitive data types? B) To read bytes from a character stream
A) BufferedReader B) ObjectInputStream C) To provide buffering for an input stream
C) DataInputStream D) InputStreamReader D) To serialize and deserialize objects
[Link] is the purpose of using [Link] of the following is NOT true about
BufferedOutputStream in Java? BufferedReader in Java?
A) To directly write bytes to a file A) It reads text from a character-input stream
B) To efficiently write bytes to an output stream B) It provides buffering for efficient reading of
by reducing the number of I/O operations characters, arrays, and lines
C) To write character data to a file C) It is synchronized, making it safe for
D) To handle exceptions during file writing multithreaded access
operations D) It is more efficient than using
[Link] of the following statements regarding InputStreamReader alone
Java I/O Streams is false? [Link] is the purpose of the ObjectOutputStream
A) Streams are uni-directional, meaning data class in Java?
flows in one direction only. A) To read objects from a binary stream
B) Java I/O Streams are only applicable to file- B) To write primitive data types to a binary
based operations. stream
C) Streams can be used for both input and C) To serialize objects into a binary stream
output operations. D) To deserialize objects from a binary stream
D) Streams are classified into two categories: [Link] of the following classes in Java is used to
byte streams and character streams. write formatted data to a text-output stream?
[Link] is the role of InputStreamReader in Java A) BufferedWriter B) PrintWriter
I/O? C) FileWriter D) OutputStreamWriter
A) To read binary data from an input source [Link] is the main advantage of using
B) To convert bytes to characters DataInputStream and DataOutputStream in
C) To read primitive data types from an input Java?
source A) They provide support for reading and writing
D) To perform encryption on input streams text files efficiently.
[Link] method is used to write data to a file B) They allow random access to a file's
using FileOutputStream in Java? contents.
A) writeData() B) appendData() C) They provide methods for reading and
C) write() D) writeToStream() writing primitive data types directly.
[Link] Java, what is the purpose of the flush() D) They support character encoding conversion
method in output streams? during I/O operations.
A) To close the output stream [Link] Java class is commonly used for reading
B) To flush any buffered data to the underlying input from the console?
stream A) Scanner B) InputStreamReader
C) To synchronize the stream with the file C) BufferedReader D) [Link]
system [Link] is the purpose of the ObjectInputStream
D) To discard any unread data in the stream class in Java?
Different types of 1/0 streams A) To read objects from a binary stream
[Link] of the following classes in Java is NOT B) To write objects to a binary stream
used for handling byte-oriented input streams? C) To serialize objects into a binary stream
A) FileInputStream B) DataInputStream D) To deserialize objects from a binary stream
C) ObjectInputStream D) InputStreamReader [Link] of the following statements about
[Link] Java class is used for handling character- FileReader and FileWriter in Java is true?
oriented input streams? A) They are used for reading and writing binary
A) InputStream B) OutputStream data.
C) Reader D) Writer B) They are subclasses of the InputStream and

ADITYA POLYTECHNIC COLLEGES Page | 15


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
OutputStream classes. E) None of the above
C) They are used for reading and writing [Link] happens if you attempt to use
character data from and to text files. [Link]().readLine() in a program
D) They support both character and byte- running in an environment where no console is
oriented I/O operations. available?
[Link] Java, which class provides support for A) It throws a NoSuchElementException.
reading character streams from byte streams, B) It returns null.
converting bytes to characters using a specified C) It waits indefinitely for a console to become
character encoding? available.
A) InputStreamReader B) BufferedReader D) It throws a NullPointerException.
C) FileReader D) CharBuffer E) None of the above
[Link] is the primary difference between [Link] of the following classes can be used to
FileInputStream and FileReader in Java? convert bytes to characters while reading from a
A) FileInputStream is used for reading binary stream in Java?
data, while FileReader is used for reading A) InputStreamReader B) BufferedReader
character data. C) PrintWriter D) FileReader
B) FileInputStream is used for reading character E) None of the above
data, while FileReader is used for reading [Link] a multithreaded Java application, what
binary data. precautions should be taken when using
C) FileInputStream provides better performance [Link]() for input/output?
than FileReader. A) Synchronize access to the console object.
D) FileReader provides support for random B) Ensure that only one thread has access to the
access to the file's contents. console at a time.
[Link] of the following classes in Java provides C) Wrap access to the console in a try-catch
support for reading and writing objects directly block to handle exceptions.
to and from a file? D) Use a separate console object for each
A) ObjectStream thread.
B) ObjectReaderWriter E) None of the above
C) ObjectIOStream [Link] is the purpose of the Console class in
D) ObjectOutputStream/ObjectInputStream Java, and why might it return null when
[Link] is the purpose of the File class in Java? accessed via [Link]()?
A) To create new directories only. A) The Console class provides methods for
B) To read and write text files. interacting with the console, and it returns null
C) To represent file and directory pathnames. when there is no console attached, such as in
D) To manage input and output streams. headless environments.
[Link] of the following methods is used to close B) The Console class is used for file I/O
a stream in Java? operations, and it returns null when the file is
A) finalize() B) closeStream() inaccessible.
C) shutdown() D) close() C) The Console class is used for GUI-based
Console input/output Streams input/output, and it returns null when the GUI is
[Link] of the following statements about not initialized.
[Link]() in Java is true? D) The Console class provides methods for
A) It is available in all versions of Java. accessing system properties, and it returns null
B) It can be used to directly read integers from when the system properties are not available.
the console. E) None of the above
C) It is a static method of the System class. [Link] of the following classes is used to read
D) It throws a NullPointerException if there is bytes from the console in Java?
no console attached. A) Scanner B) BufferedReader
E) None of the above C) InputStreamReader D) Console
[Link] of the following methods is used to write E) None of the above
formatted output to the console in Java? [Link] of the following methods is used to read
A) print() B) write() a line of text from the console using Console
C) format() D) println() class in Java?

ADITYA POLYTECHNIC COLLEGES Page | 16


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
A) readLine() B) nextLine() A) It allows random access to a file's content for
C) readString() D) readText() both reading and writing.
E) None of the above B) It provides random access to a file's
[Link] is the purpose of the [Link]() metadata.
method in Java? C) It allows random access to a file's content for
A) To create a new console object. reading only.
B) To get the standard input/output console D) It provides random access to a file's content
object. for writing only.
C) To print text to the console. [Link] does the FileChannel class provide in
D) To close the console. Java?
E) None of the above A) It provides high-level methods for file I/O
[Link] of the following statements is true about operations.
the [Link]() method? B) It represents a channel for reading bytes from
A) It returns null if there is no console available. a file.
B) It throws an exception if there is no console C) It represents a channel for reading and
available. writing bytes to a file.
C) It creates a new console if there is no console D) It provides low-level access to the operating
available. system's file system.
D) It waits for the user to input data from the [Link] Java, what is the purpose of the Files class in
console. the [Link] package?
E) None of the above A) It provides methods for creating and
[Link] of the following is true about using manipulating files and directories.
[Link] for console input in Java? B) It provides methods for reading and writing
A) It reads bytes directly from the keyboard. files using streams.
B) It provides methods for reading text directly C) It provides methods for random access to file
from the console. contents.
C) It is the preferred method for console input in D) It provides methods for compressing and
Java. decompressing files.
D) It requires wrapping with a BufferedReader [Link] is the purpose of using the FileLock class
for efficient reading of text. in Java?
E) None of the above A) It locks the entire file for exclusive access by
[Link] is the main advantage of using Console one program.
class over other methods for console B) It locks a specific region of a file for
input/output in Java? exclusive access by one program.
A) It provides better performance. C) It prevents other programs from accessing
B) It provides methods for password input the file.
without echoing characters. D) It synchronizes access to the file across
C) It is easier to use for reading and writing text multiple threads within the same program.
from/to the console. [Link] of the following methods is used to
D) It supports internationalization of console delete a file in Java?
input/output. A) [Link]() B) [Link]()
E) None of the above C) [Link]() D) [Link]()
File access operations [Link] is the purpose of the [Link]
[Link] of the following methods can be used to interface in Java?
efficiently read a large file in Java? A) It represents a file or directory path name.
A) Using FileReader with a buffer B) It provides methods for reading and writing
B) Using BufferedReader with a large buffer files.
size C) It represents a stream for reading bytes from
C) Using Scanner for reading line by line a file.
D) Using FileInputStream with a small buffer D) It represents a channel for reading and
size writing bytes to a file.
[Link] Java, what is the purpose of the [Link] method is used to determine the size of a
RandomAccessFile class? file in Java?

ADITYA POLYTECHNIC COLLEGES Page | 17


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
A) [Link]() B) [Link]() [Link] interface in
C) [Link]() D) [Link]() Java?
[Link] Java, what is the purpose of the FileVisitor A) It represents a file or directory path name.
interface in the [Link] package? B) It represents file attributes such as
A) It represents a visitor pattern for traversing a permissions and ownership.
file tree. C) It provides methods for reading and writing
B) It provides methods for asynchronous file files.
I/O operations. D) It represents a visitor pattern for traversing a
C) It represents a channel for reading and file tree.
writing bytes to a file. [Link] method is used to read the attributes of a
D) It represents a stream for reading bytes from file in Java?
a file. A) [Link]()
[Link] of the following methods is used to set B) [Link]()
the permissions of a file in Java? C) [Link]()
A) [Link]() D) [Link]()
B) [Link]() Array List and Linked List:
C) [Link]() [Link] a scenario where you need to
D) [Link]() frequently insert elements in the middle of a
[Link] is the purpose of the large collection and maintain the order. Which
[Link] interface in Java? data structure would be the most appropriate
A) It provides methods for watching changes to choice?
a directory. A) ArrayList
B) It provides methods for asynchronously B) LinkedList
reading from a file. C) Both ArrayList and LinkedList have similar
C) It represents a visitor pattern for traversing a performance for this scenario.
file tree. D) TreeSet
D) It provides methods for compressing and [Link] is the time complexity of removing an
decompressing files. element at an arbitrary index (not the beginning
[Link] method is used to rename a file in Java? or enD) in both ArrayList and LinkedList?
A) [Link]() B) [Link]() A) O(1) for both
C) [Link]() D) [Link]() B) O(n) for both
[Link] Java, what is the purpose of the C) O(1) for ArrayList, O(n) for LinkedList
[Link] class? D) O(n) for ArrayList, O(1) for LinkedList
A) It locks the entire file for exclusive access by [Link] you have a requirement to implement a
one program. stack using either ArrayList or LinkedList.
B) It locks a specific region of a file for Which data structure would be more suitable
exclusive access by one program. and why?
C) It prevents other programs from accessing A) ArrayList, because it has better memory
the file. utilization.
D) It synchronizes access to the file across B) LinkedList, because it has constant-time
multiple threads within the same program. insertion and removal at both ends.
[Link] of the following is true about the C) Both ArrayList and LinkedList are equally
[Link] suitable for implementing a stack.
interface in Java? D) It depends on the specific requirements of
A) It provides methods for reading and writing the application.
files. [Link] a scenario where you need to frequently
B) It represents basic file attributes such as size, perform binary searches on a sorted list, which
creation time, and last modified time. data structure would provide better
C) It represents a stream for reading bytes from performance?
a file. A) ArrayList
D) It provides methods for compressing and B) LinkedList
decompressing files. C) Both ArrayList and LinkedList have similar
[Link] is the purpose of the performance for binary searches.

ADITYA POLYTECHNIC COLLEGES Page | 18


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
D) TreeSet implements both the List and Queue interfaces
[Link] of the following operations has a higher and provides the flexibility of accessing
time complexity in LinkedList compared to elements either as a list or as a queue?
ArrayList? A) PriorityQueue B) LinkedList
A) Adding an element at the end of the list C) ArrayDeque D) Vector
B) Adding an element at the beginning of the [Link] of the following methods is used to
list iterate over a List in reverse order using
C) Removing an element at the end of the list ListIterator in Java?
D) Removing an element at the beginning of the A) reverse() B) previous()
list C) reverseIterator() D) descendingIterator()
[Link] you have a scenario where you need to [Link] is the time complexity of the add()
frequently iterate over the elements of a operation in ArrayList in Java, assuming the
collection in reverse order. Which data structure ArrayList does not need to resize its internal
would be more efficient? array?
A) ArrayList A) O(1) B) O(log n)
B) LinkedList C) O(n) D) O(n log n)
C) Both ArrayList and LinkedList have similar [Link] is the purpose of the Iterator interface in
efficiency for iterating in reverse order. Java?
D) Neither ArrayList nor LinkedList is suitable A) To provide methods for sorting elements in a
for this scenario. list.
Iterator List Interface: B) To allow iteration over a collection of
[Link] of the following methods is not present elements.
in the ListIterator interface in Java? C) To provide methods to access and modify
A) hasNext() B) previous() elements in a list.
C) add() D) nextIndex() D) To allow concurrent modification of a list.
[Link] Java, what does the set() method of [Link] method of the Iterator interface is used to
ListIterator do? retrieve the next element in the iteration?
A) Adds an element at the current position of A) hasNext() B) next()
the iterator. C) remove() D) get()
B) Replaces the last element returned by next() [Link] happens if you call the next() method of
or previous() with the specified element. an Iterator when there are no more elements in
C) Removes the last element returned by next() the collection?
or previous() from the list. A) It returns null.
D) Inserts the specified element at the beginning B) It throws a NoSuchElementException.
of the list. C) It returns false.
[Link] of the following statements regarding the D) It moves to the first element of the
Iterator interface in Java is correct? collection.
A) An Iterator can be bidirectional. [Link] method of the Iterator interface is used to
B) An Iterator allows the modification of remove the last element returned by the next()
elements during iteration. method from the underlying collection?
C) An Iterator is a legacy interface replaced by A) remove() B) delete()
the ListIterator interface. C) erase() D) exclude()
D) An Iterator can be obtained from any Hash Table and Hash Map:
Collection interface implementation. [Link] is the key difference between a Hash
[Link] will happen if the remove() method is Table and a HashMap in Java?
called twice successively without calling either A) Hash Table allows null keys but not null
next() or previous() in between in ListIterator? values, while HashMap allows both null keys
A) It will remove the first element in the list. and null values.
B) It will remove the last element returned by B) Hash Table is synchronized while HashMap
next() or previous(). is not synchronized.
C) It will throw an IllegalStateException. C) Hash Table allows duplicate keys but
D) It will remove every other element in the list. HashMap does not.
[Link] Java, which of the following classes D) Hash Table provides constant-time

ADITYA POLYTECHNIC COLLEGES Page | 19


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
performance for all operations while HashMap C) Overriding can only occur between classes in
does not. the same package
[Link] would you prefer to use a HashMap over D) Overriding is mandatory in Java
a Hash Table in Java? [Link] of the following statements about
A) When thread safety is not a concern interfaces in Java is true?
B) When working with a single-threaded A) An interface can extend a class.
application B) An interface can contain constructors.
C) When performance is critical and thread C) An interface can contain method bodies.
safety is not required D) An interface can have access modifiers other
D) When needing to support legacy code that than public.
relies on Hashtable [Link] access specifier allows a class to be
[Link] of the following statements is true about accessed only within the same package?
hash collisions in a HashMap? A) private B) protected
A) Hash collisions never occur in a HashMap C) public D) default (package-private)
due to the use of unique hash codes. [Link] access specifier allows a member
B) Hash collisions can occur when two different variable to be accessed only within its own class
keys hash to the same index in the HashMap. and its subclasses?
C) Hash collisions only occur when resizing the A) private B) protected
HashMap. C) public D) default (package-private)
D) Hash collisions are handled by rehashing the [Link] access specifier allows a member
entire HashMap. variable to be accessed from any class in the
[Link] is the time complexity of the get() and same package or from a subclass in any
put() operations in a HashMap in the average package?
case? A) private B) protected
A) O(1) for both get() and put() C) public D) default (package-private)
B) O(n) for get() and O(1) for put()
C) O(n) for both get() and put() KEY:
D) O(log n) for both get() and put()
[Link] a HashMap, what happens if two different Q A Q A Q A
keys produce the same hash code? n n n n n n
A) The newer value replaces the older value o s o s o s
associated with the same key. 1 C 1 C 3 D
B) The older value is preserved, and the newer 6 1
value is discarded. 2 B 1 D 3 C
C) Both values are stored in the HashMap with 7 2
separate entries. 3 D 1 A 3 D
D) The HashMap throws a runtime exception. 8 3
[Link] interface does HashMap implement in 4 D 1 D 3 C
Java? 9 4
A) Map B) Dictionary 5 A 2 C 3 C
C) Hashtable D) Collection 0 5
[Link] of the following constructor signatures 6 C 2 A 3 D
demonstrate constructor overloading? 1 6
A) public Example() 7 B 2 D 3 B
B) public Example(int x) 2 7
C) public Example(int x, int y) 8 D 2 B 3 B
D) All of the above 3 8
[Link] of the following is true about method 9 C 2 C 3 A
overriding in Java? 4 9
A) Overriding allows a subclass to define a 1 A 2 D 4 A
method with the same signature as a method in 0 5 0
its superclass 1 B 2 B 4 B
B) Overriding is not allowed in Java

ADITYA POLYTECHNIC COLLEGES Page | 20


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
1 6 1 9 8 1
1 C 2 D 4 D 7
2 7 2 6 A 8 D 1 D
1 A 2 C 4 A 0 9 1
3 8 3 8
1 B 2 C 4 A 6 B 9 B 1 A
4 9 4 1 0 1
1 C 3 A 4 D 9
5 0 5 6 A 9 A 1 C
Q A Q A Q A 2 1 2
n n n n n n 0
o s o s o s 6 A 9 B 1 A
4 B 7 A 1 B 3 2 2
6 5 0 1
4 6 B 9 B 1 C
4 B 7 D 1 B 4 3 2
7 6 0 2
5 6 C 9 D 1 C
4 B 7 B 1 B 5 4 2
8 7 0 3
6 6 B 9 A 1 D
4 A 7 B 1 C 6 5 2
9 8 0 4
7 6 A 9 B 1 C
5 C 7 A 1 A 7 6 2
0 9 0 5
8 6 D 9 D 1 A
5 B 8 A 1 C 8 7 2
1 0 0 6
9 6 C 9 D 1 A
5 A 8 D 1 B 9 8 2
2 1 1 7
0 7 A 9 A 1 B
5 A 8 D 1 C 0 9 2
3 2 1 8
1 7 B 1 A 1 D
5 C 8 B 1 D 1 0 2
4 3 1 0 9
2 7 C 1 A 1 B
5 A 8 B 1 A 2 0 3
5 4 1 1 0
3 7 B 1 B 1 C
5 A 8 D 1 A 3 0 3
6 5 1 2 1
4 7 C 1 A 1 B
5 B 8 B 1 C 4 0 3
7 6 1 3 2
5
5 B 8 A 1 A
8 7 1
6
5 B 8 C 1 A Qno Ans Qno Ans Qno Ans

ADITYA POLYTECHNIC COLLEGES Page | 21


Name of the Dept. Computer Engineering Name of the Subject. Java
Programming
133 B 162 A 191 D
134 C 163 B 192 C
135 B 164 A 193 B
136 B 165 D 194 D
137 B 166 B 195 A
138 C 167 B 196 B
139 B 168 A 197 B
140 D 169 D 198 B
141 C 170 A 199 A
142 C 171 B 200 B
143 C 172 A 201 C
144 C 173 A 202 B
145 B 174 C 203 A
146 C 175 A 204 A
147 A 176 C 205 A
148 D 177 A 206 D
149 C 178 B 207 A
150 A 179 B 208 D
151 A 180 B 209 D
152 D 181 B 210 B
153 C 182 A 211 C
154 D 183 B
155 C 184 D
156 C 185 B
157 D 186 A
158 A 187 B
159 B 188 B
160 A 189 A
161 D 190 B

ADITYA POLYTECHNIC COLLEGES Page | 22

You might also like