Gujarat Board, Class XII, Computer Science, Year 2022, Java Questions with Answers and Explanation
1) Which of the following property keeps the data safe from
unintended actions and inadvertent access by outside objects?
(A) Inheritance
(B) Composition
(C) Polymorphism
(D) Encapsulation
Answer: (D) Encapsulation
Explanation:
Encapsulation is the property that keeps the data safe from
unintended actions and inadvertent access by outside objects. It involves
bundling the data (attributes) and methods that operate on the data into a
single unit known as a class. Access to the data is restricted to the methods
defined within the class, and the internal details of the object are hidden
from the outside world. This helps in achieving data security and prevents
unintended modifications.
2) Which of the following is a program's capability of
being moved easily from one computer system to another?
(A) General purpose programming
(B) In-built variable
(D) Modular
(C) Platform-independence
Answer: (C) Platform-independence
Explanation:
Platform independence refers to a program's capability of
being moved easily from one computer system to another without modification.
This is achieved through concepts like the Java Virtual Machine (JVM) and the
"write once, run anywhere" philosophy in Java. Java programs can run
on any device that has a Java Virtual Machine, making them
platform-independent.
3) In Java, which of the following statements enable to
control the flow of execution?
(A) Block of statements
(B) Control structures
(C) Operators
(D) Associativity of operators
Answer: (B) Control structures
Explanation:
Control structures in Java, such as if statements, loops,
and switch statements, enable the programmer to control the flow of execution
in a program. These structures determine the order in which statements are
executed based on conditions or iterations.
4) In Java, which logical operator results in complemented
result?
(A) XOR
(B) OR
(C) AND
(D) NOT
Answer: (D) NOT
Explanation:
The logical NOT operator in Java is represented by
"!" and it results in the complemented result. If a condition is true,
the NOT operator makes it false, and if a condition is false, the NOT operator
makes it true.
5) In Java, what will be the value of y, where y = 7 + ++x
and x = 3?
(A) 11
(B) 4
(C) 10
(D) 3
Answer: (A) 11
Explanation:
Given x = 3, and the expression y = 7 + ++x. The
pre-increment (++x) means that x is incremented before its value is used in the
expression. So, x becomes 4, and then y = 7 + 4 = 11.
6) In Java, which kind of comment begin with /* and ends
with */?
(A) Documentation comment
(B) Multi-line comment
(C) Single-line comment
(D) Object-oriented comment
Answer: (B) Multi-line comment
Explanation:
In Java, comments that begin with `/*` and end with `*/`
are multi-line comments. These comments can span multiple lines and are used
for providing explanations, annotations, or disabling portions of code for
testing or debugging.
7) In Java, which of the following is a proper example of
unicode literal?
(A) \0457
(B) \0b123
(C) \0xABC
(D) \u00E9
Answer: (D) \u00E9
Explanation:
In Java, the `\u` escape sequence is used for representing
Unicode characters. In the given example, `\u00E9` represents the Unicode
character with hexadecimal value 00E9, which is the character 'é'.
8) In Java, which of the following can take different data
values at different times during the execution of programs?
(A) Data types
(B) Operator
(C) Variable
(D) Control structures
Answer: (C) Variable
Explanation:
In Java, a variable is a named storage location that can
hold different data values at different times during the execution of a
program. The type of data that a variable can hold is determined by its data
type.
9) In Java, which of the following data type has range of
values from -32768 to 32767?
(A) int
(B) short
(C) byte
(D) Boolean
Answer: (B) short
Explanation:
In Java, the "short" data type has a range of
values from -32768 to 32767.
10) In Java, which of the following file will not be
created, if the program does not compile successfully?
(A) .htm
(B) .java
(C) .class
(D) .html
Answer: (C) .class
Explanation:
If a Java program does not compile successfully, the
compiler will not generate the bytecode file (with a .class extension). The
.java file contains the source code, and the .class file is the compiled
bytecode file. If there are compilation errors, the .class file won't be
created.
11) In Java, which of the following is a post-test loop
construct where statements of loop are executed at least once?
(A) do...while
(B) while
(C) for
(D) switch statement
Answer: (A) do...while
Explanation:
The do...while loop is a post-test loop in Java. It
guarantees that the loop statements are executed at least once because the
condition is checked after the execution of the loop body. If the condition is
true, the loop continues; otherwise, it exits.
12) Which of the following is used to provide a
user-defined no-argument constructor in Java?
(A) <objectname> [] ();
(B) <objectname> {} ();
(C) <objectname> {} [];
(D) <classname> () {};
Answer: (D) <classname> () {};
Explanation:
To provide a user-defined no-argument constructor in Java,
you use the syntax:
<objectname> () {
// constructor code
}
This constructor does not take any arguments and is invoked
when an object is created.
13) In Java, which of the following is not a part of four
P's of protection?
(A) Public
(B) Private
(C) Primary
(D) Package
Answer: (C) Primary
Explanation:
The four P's of protection in Java are:
1. Public
2. Private
3. Protected
4. Package (default, no modifier)
"Primary" is not a part of the commonly used
access modifiers in Java.
14) In Java, which kind of variables are not initialized by
default values?
(A) Instance variables
(B) Local variables
(C) Global variables
(D) Class variables
Answer: (B) Local variables
Explanation:
In Java, local variables are not automatically initialized
by default values. They must be explicitly assigned a value before they are
used. Instance variables and class variables are automatically initialized to
default values if not explicitly initialized by the programmer. Global
variables are not a distinct concept in Java.
15) In Java, which keyword is used to define a class?
(A) class
(B) super
(C) new
(D) extends
Answer: (A) class
Explanation:
The keyword "class" is used to define a class in
Java.
16) In Java, what is the name of the special portion of
memory where the objects live?
(A) Sub class
(B) Super class
(C) Garbage collector
(D) Heap
Answer: (D) Heap
Explanation:
In Java, the heap is the special portion of memory where
objects are stored. The Java Virtual Machine (JVM) manages the heap memory, and
the garbage collector is responsible for reclaiming memory occupied by objects
that are no longer in use.
17) In Java, in which kind of protection the methods and
variables are directly accessible only by the methods defined within a class?
(A) Protected
(B) Package
(C) Public
(D) Private
Answer: (D) Private
Explanation:
In Java, when a method or variable is declared as private,
it means that it is accessible only within the class where it is declared.
Private members are not accessible from outside the class, providing a high
level of encapsulation and data hiding.
18) In Java, which method is written when we want to allow
private data to be modified by others?
(A) Inherited
(B) Mutator
(C) Accessor
(D) Aggregated
Answer: (B) Mutator
Explanation:
A mutator method, also known as a setter method, is used in
Java to modify the private data members of a class. It provides a way for
external classes to update or set the values of private fields in an object.
The mutator method conventionally starts with the prefix "set"
followed by the name of the attribute it is modifying.
19) In Java, which kind of relationship is shared between
two classes in Inheritance?
(A) a-part-of
(B) has-a
(C) is-a
(D) not-a
Answer: (C) is-a
Explanation:
In Java, the "is-a" relationship represents
inheritance. When one class extends another, it signifies that the subclass
"is-a" type of the superclass. For example, if Class B extends Class
A, we say that B is a subclass of A, and A is the superclass of B. This is a
form of "is-a" relationship, indicating that objects of Class B are
also objects of Class A.
20) In Java which keyword is used to define class variables
and class methods?
(A) static
(B) extends
(C) new
(D) auto
Answer: (A) static
Explanation:
In Java, the `static` keyword is used to define class
variables and class methods. Class variables are shared among all instances of
a class, and class methods belong to the class rather than any specific
instance. The `static` keyword indicates that a variable or method is
associated with the class itself rather than with instances of the class.
21) In Java, in which step of object creation, keyword new
is used to create the object by allocating memory?
(A) Verification
(B) Declaration
(C) Initialization
(D) Instantiation
Answer: (D) Instantiation
Explanation:
In Java, the keyword `new` is used during the instantiation
step of object creation. Instantiation involves creating an instance (object)
of a class and allocating memory for that instance. The `new` keyword is
followed by the class constructor, which is responsible for initializing the
newly created object.
22) In Java, which of the following is a correct syntax to
declare array?
(A) <data type> <class name> [];
(B) <array name> <data type> [];
(C) <data type> <array name> [];
(D) <data type> <object name> [];
Answer: (C) <data type> <array name> [];
Explanation:
The correct syntax to declare an array in Java is to
specify the data type, followed by the array name and square brackets []. The
square brackets indicate that the variable is an array.
23) In Java, which arguments are required when we want to
sort partial array from element at index start to element at index (last-1)?
(A) array, start, last
(B) array, last, value
(C) array, start, last, value
(D) start, last, value
Answer: (A) array, start, last
Explanation:
When you want to sort a partial array from the element at
index 'start' to the element at index '(last-1)', you need to pass the array
along with the start and last indices as arguments.
24) In Java, how many bytes of contiguous memory locations
will be occupied by following array?
int marks [ ] [ ] = new int [4][3];
(A) 7 bytes
(B) 60 bytes
(C) 12 bytes
(D) 48 bytes
Answer: (D) 48 bytes
Explanation:
The array `int marks[][] = new int[4][3];` is a 2D array
with 4 rows and 3 columns. Each element of the array is of type `int`, which
typically takes 4 bytes in Java. Therefore, the total memory occupied by the
array can be calculated as:
Total bytes = Number of rows * Number of columns * Size of
each element
= 4 * 3 * 4
= 48 bytes
25) In Java, which classes are used to handle two types of
strings?
(A) StringArray and StringBuffer
(B) java.string and java.util
(C) String and StringBuffer
(D) java.io and java.string
Answer: (C) String and StringBuffer
Explanation:
In Java, the `String` class is used to handle sequences of
characters as immutable objects, and the `StringBuffer` class is used to handle
mutable sequences of characters. Both classes provide methods for various
string operations, but `StringBuffer` allows modifications to the content,
making it mutable, while `String` objects are immutable.
26) In Java, which method of Date class returns number of
milliseconds since January 1, 1970 GMT?
(A) String toString ()
(B) Date()
(C) long getTime ( )
(D) Date (long elapsed Time)
Answer: (C) long getTime()
Explanation:
The `getTime()` method of the `Date` class in Java returns
the number of milliseconds since January 1, 1970, 00:00:00 GMT.
27) Which method of string class in Java returns an array
of characters as bytes from invoking string?
(A) int length ()
(B) byte [] getBytes()
(C) string concat (string str)
(D) char indexAt (int index)
Answer: (B) byte [] getBytes()
Explanation:
The `getBytes()` method of the `String` class in Java
returns an array of bytes representing the characters of the string.
28) In Java, which Array class method is used to search an
element in an array?
(A) binarySearch()
(B) SearchString()
(C) Search()
(D) LinearSearch()
Answer: (A) binarySearch()
Explanation:
The `binarySearch()` method of the `Arrays` class in Java
is used to search for the specified element in the array using binary search.
29) In Java, which of the following method will assign
value 5 to all elements of list array?
(B) fill (list, 1, 6, 5)
(A) fill (5, list)
(C) fill (list, 2, 6, 5)
(D) fill (list, 5)
Answer: (D) fill (list, 5)
Explanation:
The `fill()` method in Java is used to fill the specified
range of the array with the specified value. In this case, it would assign the
value 5 to all elements of the `list` array.
30) In Java, which method of string class returns 0, >0,
<0 integer if invoking string is equal to, greater than or less than str
respectively?
(A) string toLowerCase()
(B) boolean equals(string str)
(C) int compareTo(string str)
(D) string toUpperCase()
Answer: (C) int compareTo(string str)
Explanation:
The `compareTo()` method in Java is used to compare two
strings lexicographically. It returns an integer value that indicates whether
the invoking string is less than, equal to, or greater than the specified
string. If the result is 0, the strings are equal. If the result is positive,
the invoking string is lexicographically greater. If the result is negative,
the invoking string is lexicographically less than the specified string.
31) In Java, which of the following constructor creates a
string object that refers to the literal specified in argument?
(A) string (string literal)
(B) string (string strobj)
(C) string ()
(D) string (char ary [])
Answer: (A) string (string literal)
Explanation:
The constructor `string (string literal)` in Java creates a
new string object that refers to the specified string literal. For example:
String str = new String("Hello"); // Using
string constructor with a string literal
32) In Java, 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 statement
(B) finally
(C) catch
(D) throws clause
Answer: (D) throws clause
Explanation:
The `throws` clause in Java is used in a method declaration
or constructor declaration to inform that the code within the method or
constructor may throw an exception. It specifies the exceptions that the method
or constructor can potentially throw, allowing the calling code to handle those
exceptions or propagate them further.
Example:
public void myMethod() throws MyException {
// Method code that may throw MyException
}
33) In Java, which block is generally used to clean up at
the end of executing a try block?
(A)
finally (B)
multiple catch
(C)
catch (D)
throws
Answer: (A) finally
Explanation:
The `finally` block in Java is generally used to clean up
or perform cleanup operations that should be executed regardless of whether an
exception is thrown or not. It is associated with a try-catch block and is
guaranteed to be executed after the try block, regardless of whether an
exception is caught or not. The finally block is useful for releasing
resources, closing files, or performing other cleanup tasks.
34) In Java, which of the following is an object - oriented
technique for managing errors?
(A) Strings
(B) Exception handling
(C) Array
(D) Operators
Answer: (B) Exception handling
Explanation:
Exception handling is an object-oriented technique for
managing errors in Java. It allows you to handle runtime errors, also known as
exceptions, in a structured and controlled manner. The key components of
exception handling in Java include try, catch, throw, and finally. Using these
constructs, you can write code that gracefully handles errors and provides
appropriate responses, improving the robustness and reliability of your Java
programs.
35) In Java, which kind of exception will occur when
someone attempts to divide any number by 0?
(A) NumberFormatException
(B) FileNotFoundException
(C) ArrayIndexOutOfBoundsException
(D) ArithmeticException
Answer: (D) ArithmeticException
Explanation:
In Java, attempting to divide any number by 0 results in an
ArithmeticException. This exception is thrown at runtime when an arithmetic
operation (such as division or modulo) produces an undefined result, such as
division by zero.
36) In how many categories Java errors can be broadly
classified?
(A) Two
(B) Three
(C) Four
(D) One
Answer: (A) Two
Explanation:
Java errors can be broadly classified into two categories:
1. Compile-time errors: These errors occur during the
compilation of the program. They are also known as syntax errors and prevent
the program from being compiled successfully.
2. Runtime errors: These errors occur during the execution
of the program. They are also known as exceptions and can lead to abnormal
program termination if not handled properly.
37) In Java, which of the following is the correct syntax
of the try block?
(A) try (Exception_ Type Exception _ Object)
{
//
code to handle the exception
}
(B) try
{
//
code to handle the exception
}
(C) try
{
//
set of statements that may generate exception
}
(D) try
{
//
clean-up code to be executed last
}
Answer: (C)
try
{
// set of statements that may generate
exception
}
Explanation:
The correct syntax of the `try` block in Java is shown in option
(C). The `try` block is used to enclose a set of statements where an exception
might occur. If an exception occurs within the `try` block, it is caught by an
associated `catch` block or handled by a `finally` block.
38) Which of the following is used as a separator between
fields of a record?
(A) variable
(B) delimiter
(C) path
(D) data type
Answer: (B) delimiter
Explanation:
- In the context of records, a delimiter is a character or
sequence of characters used to separate fields within a record.
- It helps in distinguishing the boundaries of different
fields in a record, allowing for proper parsing or processing of the data.
39) In Java, which method of scanner class returns true if
there is a token in input?
(A) boolean hasNext ( )
(B) float nextFloat ( )
(C) string next ( )
(D) string nextLine ( )
Answer: (A) boolean hasNext()
Explanation:
- In Java, the `hasNext()` method of the `Scanner` class is
used to check if there is another token (input) available. It returns `true` if
there is a token and `false` otherwise.
- This method is often used in a loop to iterate through
the tokens in the input.
40) In Java, 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) Text
files (B)
Binary files
(C) File
class (D)
Stream
Answer: (D) Stream
Explanation:
- In Java, a stream is an abstract representation of an
input or output device that is used as a source or destination for data.
- Streams are used for I/O operations in Java to handle
reading from or writing to different data sources, such as files, network
connections, or in-memory data structures.
- The `File` class in Java represents a file or directory
pathname and is often used in conjunction with streams for file I/O operations.
41) In Java, which method of File class returns the name of
files and directories in a directory?
(A) boolean exists
() (B)
string getName ()
(C) File[]
listFiles() (D)
string [] list ()
Answer: (C) `File[] listFiles()`
Explanation:
- The `listFiles()` method of the `File` class in Java
returns an array of `File` objects representing the files and directories in
the specified directory.
- The method `string[] list()` is also available, but
`File[] listFiles()` provides more detailed information about the files and
directories, including `File` objects for each entry.
42) In which kind of storage, values stored in variables
are lost when a computer is shut down?
(A) Non-volatile
(B) Volatile
(C) Permanent storage
(D) Both (A) and (C)
Answer: (B) Volatile
Explanation:
- Volatile storage refers to memory that is temporary and
loses its contents when the power is turned off.
- Non-volatile storage retains its data even when the power
is turned off. Examples include hard disk drives, solid-state drives, and flash
memory.
43) In Java, which of the following is a subclass of
InputStream and is generally used to read byte data from the files?
(A) Console class
(B) FileOutputStream
(C) FileInputStream
(D) Scanner class
Answer: (C) FileInputStream
Explanation:
- `FileInputStream` is a subclass of `InputStream` in Java
and is commonly used to read byte data from files.
- `FileOutputStream` is used for writing byte data to
files.
- `Scanner` class is used for reading formatted input, and
`Console` class provides methods to read characters and strings from the
console.
44) In OOP, which of the following describes a group of
objects with similar attributes and common behavior?
(A) Attributes
(B) Data members
(C) Object
(D) Class
Answer: (D) Class
Explanation:
- In object-oriented programming (OOP), a class is a
blueprint or a template for creating objects. It defines a group of objects
with similar attributes (data members) and common behavior (methods or
functions).
- Objects are instances of classes, and each object created
from a class has its own set of values for the attributes defined by the class.
The class encapsulates the properties and behavior that are shared by its
objects.
45) What is the full form of OMG?
(A) Omega Modelling Group
(B) Object Modelling Group
(C) Object Management Group
(D) Old Management Group
Answer: (C) Object Management Group
Explanation:
- OMG (Object Management Group): OMG is an international,
open-membership, not-for-profit technology standards consortium. It was founded
in 1989 and is responsible for setting and maintaining industry standards
related to object-oriented systems and model-driven architecture. OMG plays a
key role in the development and adoption of various standards, including the
Unified Modeling Language (UML) and the Common Object Request Broker
Architecture (CORBA).
46) Which of the following property of OOP language, is a
process of representing the essential features of the objects without including
implementation detail?
(A) Data
Abstraction (B)
Messaging
(C)
Encapsulation (D)
Aggregation
Answer: (A) Data Abstraction
Explanation:
- Data Abstraction: Data abstraction is a fundamental
concept in object-oriented programming (OOP) that involves representing the
essential features of an object while hiding the implementation details. It
allows developers to focus on what an object does rather than how it achieves
its functionality. Abstraction provides a way to model real-world objects in a
software system without exposing all the intricacies of their internal
workings.
- Messaging: Messaging is more related to the communication
between objects. While messaging is a crucial aspect of OOP, it specifically
deals with how objects interact and communicate with each other through method
calls.
- Encapsulation: Encapsulation involves bundling the data
(attributes) and methods (functions) that operate on the data into a single
unit or class. While related to abstraction, encapsulation is more about
grouping and protecting the internal state of an object.
- Aggregation: Aggregation is a relationship where one
class is part of another class. It involves forming a whole-part relationship
between objects. Aggregation is not directly related to the process of
representing essential features without including implementation details.
In summary, Data Abstraction best describes the process of
representing the essential features of objects without exposing their
implementation details in OOP.
47) In which of the following, the class that forms part of
the owner class can exist independently?
(A) Inheritance
(B) Aggregation
(C) Composition
(D) Polymorphism
Answer: (B) Aggregation
Explanation
In object-oriented programming, **aggregation** is a
relationship where one class contains an object of another class. The class
that contains the object is considered the **owner** class, and the class whose
object is being contained is the **part** class. The key point in aggregation
is that the part class can exist independently of the owner class.
For example, consider a scenario where you have a `Car`
class and a `Engine` class. The `Car` class can have an instance variable of
type `Engine`. This relationship is an example of aggregation. The `Engine`
class can exist on its own, and it can be part of different cars.
In the context of the question, the class that forms part
of the owner class (like the `Engine` class in the example) can exist
independently, making it an aggregation relationship.
48) In which kind of inheritance, a class can be derived
using more than one parent classes?
(A) Two-way Inheritance
(B) Multilevel Inheritance
(C) Multiple Inheritance
(D) Multiway Inheritance
Answer: (C) Multiple Inheritance
49) In object-oriented programming, which feature allows
defining more than one method having same name but different signature in a
single class?
(A) Aggregation
(B) Function overloading
(C) Operator overloading
(D) Composition
Answer: (B) Function overloading
50) In which kind of programming, the focus is on writing
functions or procedures which operate on data?
(A) Procedural
programming (B)
Pre-oriented programming
(C) Object-oriented programming (D)
Post-oriented programming
Answer: (A) Procedural programming