Gujarat Board, Class XII, Computer Science, Year 2023, Java Questions with Answers and Explanation
1) Which of the following keeps the data safe from unintended actions and inadvertent access by outside Objects?
(A) Data
Abstraction
(B)
Polymorphism
(C) Aggregation
(D)
Encapsulation
Answer: (D) Encapsulation
Explanation:
Encapsulation is a fundamental concept in object-oriented programming that involves bundling data (attributes) and the methods (functions) that operate on the data into a single unit known as a class. It helps keep the data safe from unintended actions and inadvertent access by outside objects. Encapsulation achieves this by restricting direct access to the internal details of an object and providing controlled access through well-defined interfaces (methods).
2) Which
is a general concept used to embody all the common features of a particular set
of objects?
(A) Class
(B)
Behaviour
(C)
Attributes
(D) Method
Answer: (A) Class
Explanation:
A class is a general concept used to embody all the common features of a particular set of objects in object-oriented programming. It defines a blueprint or template for creating objects. Objects are instances of a class, and a class encapsulates the common attributes (data) and behaviors (methods) that are shared among its objects.
3) Which
of the following statement/s is/are true for object oriented programming
language?
(i) In it,
focus is on writing functions or procedures.
(ii) It
enables the programmer to create modular, reusable and extendable code.
(iii) It uses
object as its fundamental building block.
(A) Only
(i) and (ii) (B)
Only (ii) and (iii)
(C) Only
(i) and (iii) (D)
(i), (ii) and (iii)
Answer: (B) Only (ii) and (iii)
Explanation:
(i) The statement "focus is on writing functions or procedures" is not true for object-oriented programming (OOP). OOP focuses on creating and working with objects, which encapsulate both data and the procedures that operate on that data.
(ii) OOP enables the programmer to create modular, reusable, and extendable code. This is one of the key principles of OOP, where code is organized into classes and objects, promoting modularity and reusability.
(iii) OOP uses objects as its fundamental building blocks. Objects encapsulate data and the methods (functions) that operate on that data. The use of objects allows for a more natural representation of real-world entities in the code.
4) Which symbol represent package visibility in class diagram?
(A) -
(hyphen)
(B) #
(hash)
(C) +
(plus)
(D) ~
(tilde)
Answer: (B) # (hash)
Explanation:
5) What is
the full form of UML?
(A) Universal
Modified Language
(B)
Unified Modified Language
(C)
Unified Modelling Language
(D)
Universal Modelling Language
Answer: (C) Unified Modelling Language
Explanation:
6) In Aggregation
which kind of relationship is shared between two classes?
(A) non-exclusive
(B)
is-a-kind-of
(C)
exclusive
(D) is-a
Answer: (A) non-exclusive
Explanation:
7) In
object-oriented programming which of the following allows defining more than
one method having same name but different signatures?
(A)
Messaging
(B) Method
overloading
(C) Function
overloading
(D) Both
(B) & (C)
Answer: (D) Both (B) & (C)
Explanation:
In object-oriented programming, the feature that allows defining more than one method with the same name but different signatures (parameter types or number of parameters) is known as method overloading. Both terms "method overloading" and "function overloading" are used interchangeably, and they refer to the same concept. This feature provides flexibility and makes the code more readable by allowing the use of the same method name for different behaviors based on the parameters passed to it.
8) In
class diagram, which symbol represent inheritance?
(A) Empty
diamond
(B) An
arrow pointing to super class
(C) Filled
diamond
(D) An
arrow pointing to subclass
Answer: (B) An arrow pointing to super class
9) Which
of the following is not a valid variable in Java?
(A)
birth_date
(B) $price
(C) 4me
(D)
callcost
Answer: (C) 4me
Explanation:
In Java, variable names cannot start with a digit. Option (C) "4me" violates this rule as it starts with the digit '4'. Variable names in Java must begin with a letter, underscore (_), or dollar sign ($) and can be followed by letters, digits, underscores, or dollar signs. Therefore, "4me" is not a valid variable name.
10) Match
the following for proper examples of integer literals.
Type of number Example
(i) Hexadecimal (p) 0b10110
(ii) Octal (q)
-17777
(iii) Binary (r)
0XFF7A
(iv) Decimal (s)
045
(A) (i)
-> (r), (ii) -> (s), (iii) -> (p), (iv) -> (q)
(B) (i)
-> (p), (ii) -> (q), (iii) -> (r), (iv) -> (s)
(C) (i)
-> (s), (ii) -> (r), (iii) -> (q), (iv) -> (p)
(D) (i)
-> (q), (ii) -> (p), (iii) -> (s), (iv) -> (r)
Answer: (A) (i) -> (r), (ii) -> (s), (iii) -> (p), (iv) -> (q)
Explanation:
(i) Hexadecimal: Prefix with "0x" or "0X," e.g., 0xFF7A.
(ii) Octal: Prefix with "0," e.g., 045.
(iii) Binary: Prefix with "0b" or "0B," e.g., 0b10110.
(iv) Decimal: Regular number without any prefix, e.g., -17777.
11) What will
be the value of y, where x=5 and y=4 + x++?
(A) 4
(B) 9
(C) 10
(D) 6
Answer: (B) 9
Explanation:
In the expression `y = 4 + x++`, the post-increment operator (`x++`) is used. It means that the current value of `x` (which is 5) will be used in the expression, and then `x` will be incremented by 1.
So, `y` is calculated as `4 + 5` (current value of `x`) = 9.
12) Which
of the following expression is same as q && = p
(A) q &
p
(B) p
&& q
(C) q == p
(D) q = q
&& p
Answer: (D) q = q && p
13) What
will be the result of arithmetic expression -25.8 % 7?
(A) 4.8
(B) -4.8
(C) 4
(D) -4
Answer: (B) -4.8
14) Which
kind of comments are used for creating API documentation from the code?
(A) Multi-line
comments
(B)
Documentation comments
(C)
Single-line comments
(D) Classic
comments
Answer: (B) Documentation comments
Explanation:
15) In
Java, what is the range of values for 'short' data type?
(A) -32768
to 32767
(B) -128
to 127
(C) 16 -
bit unicode character
(D) true,
false
Answer: (A) -32768 to 32767
Explanation:
16) What will
be the extension of file, when the program gets compiled without errors?
(A) .java
(B) .
javac
(C) .class
(D) .tex
Answer: (C) .class
Explanation:
17) Which
loop will evaluate the test expression after executing the statements in a
loop?
(A) while
(B) for
(C) do ...
while
(D) switch
Answer: (C) do ... while
Explanation:
In a do...while loop, the statements inside the loop are executed at least once before the test expression is evaluated. After executing the statements, the test expression is checked. If the test expression is true, the loop continues to execute. If the test expression is false, the loop terminates.
18) What is
used when there are many alterative actions to be taken depending upon the
value of a variable or expression?
(A) for
loop
(B) while loop
(C) do...while
loop
(D) switch
statement
Answer: (D) switch statement
19) Which methods
are used to define behaviour of an Object?
(A) Class
methods
(B) Instance methods
(C) Object
methods
(D) File
methods
Answer: (B) Instance methods
Explanation:
20) Which
part of Java looks for unused objects and reclaims the memory that those
objects are using?
(A) garbage
collector
(B) heap
(C) block
(D) object
Answer: (A) garbage collector
Explanation:
21) Which methods
are global to the class itself and available to any other classes or objects?
(A) Class
methods
(B) Instance
methods
(C) Object
methods
(D) Variable
methods
Answer: (A) Class methods
Explanation:
22) Which
of the following variables are created when there is a block is started and
destroyed when the method or block has completed?
(A)
Instance variables
(B) Class
variables
(C) Local
variables
(D) Global
variables
Answer: (C) Local variables
Explanation:
Local variables in Java are variables that are declared within a method, constructor, or block of code. They are created when the block or method is entered and destroyed when the block or method is exited. Local variables have a limited scope and are only accessible within the block or method in which they are declared.
23) Which of
the following is a proper way to provide a user-defined no-argument
constructor?
(A)
(classname) { } ( );
(B) <classname>
{ } ( );
(C) {class
name} ( ) ( );
(D)
<classname> ( ) { };
Answer: (D) <classname> ( ) { };
Explanation:
In Java, a user-defined no-argument constructor is created using the following syntax:
public class ClassName {
// Other class members and methods
// User-defined no-argument constructor
public ClassName() {
// Constructor code
}
}
24) Which
visibility modifier seems extremely restrictive and provides data encapsulation?
(A)
Protected
(B)
Private
(C) Public
(D)
Package
Answer: (B) Private
Explanation:
In Java, the `private` visibility modifier is the most restrictive and provides the highest level of data encapsulation. When a member (variable or method) is declared as private, it can only be accessed within the same class and is not visible to other classes. This helps in hiding the internal implementation details and ensures that the data is encapsulated within the class.
25) In Java,
in class definition what is used to create a subclass?
(A)
extends
(B) super
(C) new
(D) Dot operator
Answer: (A) extends
Explanation:
In Java, the `extends` keyword is used to create a subclass. When a class is defined using the `extends` keyword followed by the name of another class, it means that the new class inherits the properties and behaviors of the existing class, making it a subclass of that class.
So, the correct answer is (A) extends.
26) Which
keyword returns a reference to an object that represents an instance of the
class?
(A) super
(B) static
(C) new
(D)
extends
Answer: (C) new
Explanation:
In Java, the `new` keyword is used to create an instance of a class and allocate memory for the object. When you use `new` followed by the name of a class, it creates a new object of that class and returns a reference to the newly created object.
27) Which
of the following can not be invoked explicitly elsewhere in the program?
(A) instance
methods
(B) new
operator
(C)
variables
(D) constructors
Answer: (D) constructors
Explanation:
Constructors are special methods in Java that are used for initializing objects. They are automatically called when an object is created using the `new` keyword, and their purpose is to set up the initial state of the object. Constructors cannot be invoked explicitly elsewhere in the program; their invocation is tied to the creation of objects.
28) Which of
the following are used to access or modify attributes?
(A) class
(B)
methods
(C)
objects
(D) variables
Answer: (B) methods
Explanation:
Methods in Java are used to define the behavior of objects. They can include code to access or modify attributes (variables) of a class. Through methods, you can encapsulate the logic that operates on the attributes, allowing controlled access to the data.
29) Which
of the following is the correct syntax to declare a 1-D array?
(A)
{datatype} [] {arrayname} []
(B) <datatype>
{} <arrayname> {}
(C) <datatype>
<arrayname> []
(D) [datatype]
<> [arrayname]
Answer: (C) <datatype> <arrayname> []
Explanation:
The correct syntax to declare a 1-D array in Java is:
<datatype> <arrayname> [];
30) Which
method of Arrays class is used to search an element in an array?
(A) Linear
Search ( )
(B) Search
( )
(C) Array
Search ( )
(D) Binary
Search ( )
Answer: (D) Binary Search ( )
Explanation:
The `binarySearch()` method of the `Arrays` class in Java is used to search for an element in a sorted array. It uses a binary search algorithm to find the specified element. The method returns the index of the element if it is found; otherwise, it returns a negative value.
31) Which property
of 1-D array is used to know size of each row?
(A) length
(B) sort
(C) fill
(D) search
Answer: (A) length
Explanation:
32) Which
of the following constructor can be used to create a string object with its
initial value using ary argument?
(A) String
(char ary[])
(B) String
(String literal)
(C)
String()
(D) String
(String strObj)
Answer: (A) String (char ary[])
Explanation:
The constructor `String(char ary[])` is used to create a new String object and initialize it with the character array passed as an argument. It constructs a new String object containing the characters of the specified array. So, the correct option is (A) String (char ary[]).
33) Which
of the following method returns character at index position from the invoking
string?
(A) byte
Atindex ( )
(B) int indexAt(
)
(C) char charAt(int index)
(D)
boolean indexAt( )
Answer: (C) char charAt(int index)
Explanation:
34) Which
methods of Date class returns number of milliseconds since January 1, 1970 GMT?
(A) Date()
(B) Date
(long Time)
(C) long
getTime ( )
(D) void
setTime( )
Answer: (C) long getTime ( )
Explanation:
35) Which constant
of calendar class display Hour in 24-hour notation?
(A) HOUR
(B) HOUR_IN_24
(C) HOUR_AM_PM
(D) HOUR_OF_DAY
Answer: (D) HOUR_OF_DAY
Explanation:
36) How
many integer values will be stored in contiguous memory when array is created
using
int marks
[ ] [ ] = new int[6] [2];
(A) 24
(B) 12
(C) 8
(D) 15
Answer: (B) 12
Explanation:
In a 2D array, the number of integer values stored in contiguous memory is determined by the product of the number of rows and the number of columns. For the given array `int marks[][] = new int[6][2];`, there are 6 rows and 2 columns.
Number of integer values = 6 rows * 2 columns = 12
37) Which
methods are declared as static and can be referred without any object in the
class?
(A) Class
methods
(B)
Variables methods
(C) Instance
methods
(D) Array
methods
Answer: (A) Class methods
Explanation:
Class methods in Java are declared as static, and they can be invoked using the class name without creating an instance of the class. They belong to the class rather than a specific instance of the class.
Therefore, the correct option is (A) Class methods.
38) Which of
the following will fill all elements of list array with value ‘7’?
(A) fill(list,
2, 7, 7)
(B)
fill(list, 7, [6-1])
(C) fill
(list, 7)
(D) fill(list,
[6+1], 7)
Answer: (C) fill(list, 7)
Explanation:
The `fill` method in Java is used to fill all elements of an array with a specified value. The correct syntax is `Arrays.fill(array, value)`, where `array` is the array to be filled, and `value` is the value with which the array elements will be filled.
39) Which
of the following is an indication of a problem that occurs during program's
execution and usually signals an error?
(A)
Behaviour
(B) Exception
(C) Method
(D) Object
Answer: (B) Exception
Explanation:
In programming, an exception is an indication of a problem that occurs during a program's execution. It is an event or object that signals an error or exceptional condition that may need to be handled. Exceptions disrupt the normal flow of the program and typically require special handling to prevent the program from crashing.
40) which
kind of exception will occur when user type following statements in Java?
int a[ ] =
new int[4];
a[13] =
99;
(A)
ArraylndexOutOfBoundsException
(B) ArithmeticException
(C)
NullPointerException
(D)
NumberFormatException
Answer: (A) ArrayIndexOutOfBoundsException
Explanation:
The given code snippet is trying to access an element at index 13 in an array 'a' that has been declared as `new int[4]`. In Java, array indices start from 0, so the valid indices for this array are 0, 1, 2, and 3. Attempting to access an element at index 13 is beyond the bounds of the array, and it will result in an `ArrayIndexOutOfBoundsException`.
41) Which
kind of catch block has to be placed at the end of all catch blocks?
(A) finally
(B)
default
(C) null
(D) empty
42) Which
of the following will be used when we want to be sure that some particular code
is to be run, no matter what exceptions are thrown within the associated try
block?
(A) First
catch block (B)
Null catch block
(C) Finally
block (D)
Multiple catch blocks
Answer: (C) Finally block
Explanation:
The `finally` block in Java is used to ensure that a certain code block is executed, regardless of whether an exception is thrown or not. It is typically used to perform cleanup operations, such as closing files or releasing resources, and it is always executed after the try block, whether an exception is caught or not.
So, when you want to make sure that some particular code is run, no matter what exceptions are thrown within the associated try block, you use the `finally` block.
43) Which
of the following can be used in a method declaration or constructor declaration
to inform that the code within the constructor or method may throw an
Exception?
(A) throw clause
(B)
finally block
(C)
multiple try blocks
(D) throws
clause
Answer: (D) throws clause
Explanation:
In Java, the `throws` clause is used in a method declaration or constructor declaration to indicate that the code within the method or constructor may throw certain exceptions. When a method or constructor is declared with a `throws` clause, it signals that the method or constructor may throw exceptions specified in the `throws` clause, and the calling code should be prepared to handle those exceptions or propagate them further.
44) Which
of the following is a correct syntax of try block?
(A) try
{
// code to handle the exception
}
(B) try
(Exception_Type)
{
// clean-up code to be executed last
}
(C) try (Exception_Type
Exception _Object)
{
// code to handle compile time error
}
(D) try
{
// set of statements that may
generate one or more exceptions
}
{
// set of statements that may generate one or more exceptions
}
Explanation:
The correct syntax of a try block in Java is as follows:
try {
// set of statements that may generate one or more exceptions
} catch (ExceptionType1 e1) {
// code to handle exception of type ExceptionType1
} catch (ExceptionType2 e2) {
// code to handle exception of type ExceptionType2
} finally {
// clean-up code to be executed whether an exception is thrown or not
}
In a try block, you include the set of statements that may generate exceptions. The catch blocks are used to handle specific types of exceptions that may be thrown within the try block. The finally block contains clean-up code that will be executed whether an exception is thrown or not. The finally block is optional, and you can have multiple catch blocks for different exception types.
45) Which
of the following class encapsulates information about the properties of a file
or a directory?
(A)
java.io.package
(B)
java.io.File
(C)
java.io.util
(D)
java.util
Answer: (B) java.io.File
Explanation:
The `java.io.File` class in Java encapsulates information about the properties of a file or a directory. It is part of the `java.io` package and provides methods for creating, deleting, querying, and manipulating files and directories. The `File` class is commonly used for file I/O operations in Java.
46) Which
of the following method retums an array of abstract pathnames denoting the
files in the directory?
(A) String
[ ] list ( )
(B) boolean
list( )
(C) File [
] listFiles( )
(D) String
getPath ( )
Answer: (C) File[] listFiles()
Explanation:
47) Which
of the following is an abstract representation of an input or output device
that is used as a source or destination for data?
(A) stream
(B) class
(C) file
(D)
directory
Answer: (A) stream
Explanation:
48) Which
class can be used especially when the input is to be typed in hidden form?
(A)
java.util
(B)
java.scanner
(C)
java.io.Console
(D) java.io.File
Answer: (C) java.io.Console
Explanation: The `java.io.Console` class in Java can be used especially when the input is to be typed in a hidden form, such as for password entry. It provides methods like `readPassword()` that allow reading passwords without echoing characters to the console, providing a secure way to handle sensitive information like passwords.
49) Which
of the following statement is true?
(A)
Volatile storage lasts only a few seconds
(B) Volatile
storage is lost when a computer is shutdown
(C)
Computer disks are volatile storage devices
(D) All Of
the above are true
Answer: (B) Volatile storage is lost when a computer is shutdown
Explanation: Volatile storage, such as RAM (Random Access Memory), is temporary and loses its contents when the power is turned off or the computer is shut down. Non-volatile storage, on the other hand, retains its data even when the power is off, and examples include hard drives and solid-state drives.
50) Which of
the following is a valid extension of Text file?
(A) .java
(B) .jpeg
(C) .mp3
(D) .class
Answer: (A) .java
Explanation: A text file typically has a .txt extension. However, in the context of Java programming, source code files are often saved with a .java extension.