Gujarat Board, Class XII, Computer Science, 2022 July, Java Questions with Answers and Explanation
1) Which of the following is a special kind of method that is invoked when a new object is created?
(A) Console
(B) Constructor
(C) Conductors
(D) Consumer
Answer: (B) Constructor
Explanation:
In object-oriented programming, a constructor is a special method that is automatically called when an object of a class is created. It initializes the object's state and performs any necessary setup. The purpose of a constructor is to ensure that the object is in a valid state after it is created. In Java, the constructor has the same name as the class and is invoked using the `new` keyword when an object is instantiated.
2) Which
of the following statement should be added as the first non-comment or
non-blank line in the source file of java?
(A) Package
(B)
Private
(C) Public
(D)
Protocol
Answer: (A) Package
Explanation:
The `package` statement in Java is used to declare a package name for the Java class. It should be the first non-comment line in a Java source file, and it is optional. The `package` statement specifies the package to which the Java file belongs, and it helps in organizing and categorizing classes. If a Java file is part of a package, the `package` statement should precede any import statements and class declarations.
3) Highest
level of protection in Java can be achieved using which of the following
protection level?
(A)
Protocol
(B) Private
(C) Public
(D)
Protected
Answer: (B) Private
Explanation:
4) Which
of the following is the keyword used to refer a superclass constructor in
subclass constructor?
(A) name
of the superclass
(B) super
(C)
extends
(D) new
Answer: (B) super
Explanation:
In Java, the `super` keyword is used to refer to the superclass or parent class. When used in a subclass constructor, `super()` is used to call the constructor of the superclass. This is typically done as the first statement inside the subclass constructor. It allows the subclass to initialize its own instance variables and also invoke the constructor of the superclass to perform any necessary initialization of inherited members. Using `super` helps in establishing a relationship between the subclass and the superclass.
5) Which
of the following are useful to represent vector, matrix, and other multi-dimensional
data?
(A)
Variable
(B) Array
(C) Cookie
(D) static
Answer: (B) Array
Explanation:
Arrays are data structures in Java that allow you to store multiple values of the same type under a single variable name. They are useful for representing vector, matrix, and other multi-dimensional data. Arrays provide a convenient way to work with collections of values, and they can have one or more dimensions, making them suitable for various applications where structured storage of data is required.
6) In which of the following way we can create array object?
(A) marks
= new int [5];
(B) marks
new = int[5];
(C) marks
new [ ] = int[5];
(D) marks int
= new[5];
Answer: (A) marks = new int [5];
Explanation:
To create an array object in Java, you use the "new" keyword followed by the data type and the size of the array in square brackets. The correct syntax is:
dataType[] arrayName = new dataType[size];
In the given options, (A) marks = new int [5]; follows the correct syntax for creating an integer array named "marks" with a size of 5.
7) In
Java, suppose array is defined as int m1 [] = {3, 2, 4, 5, 8}; then what will be
value of m1[3]?
(A) 2
(B) 3
(C) 5
(D) 8
Answer: (C) 5
Explanation:
In Java arrays, the index starts from 0. So, m1[3] refers to the element at the 4th position in the array. In the given array m1[] = {3, 2, 4, 5, 8}, the element at index 3 is 5.
8) In Java,
if a 2D array is defined as int marks [][] = new int [5] [3]; then how many
integer values can be stored contiguous memory locations?
(A) 15
(B) 125
(C) 65
(D) 08
Answer: (A) 15
Explanation:
In a 2D array, the number of contiguous memory locations is determined by multiplying the number of rows by the number of columns. In this case, there are 5 rows and 3 columns, so the total number of integer values that can be stored is 5 * 3 = 15.
9) Which
of the following is nothing but a sequence of character?
(A)
boolean
(B) string
(C) char
(D) int
Answer: (B) string
Explanation:
10) Which
of the following string class method returns true if invoking string is same as
str?
(A)
boolean notequal (string str)
(B)
boolean neq (string str)
(C)
boolean eq (string str)
(D) boolean
equals (string str)
Answer: (D) boolean equals (string str)
Explanation:
11) Which
of the following string class method returns number of characters invoking
string?
(A) int
lengths ()
(B) int
length ()
(C) int
long ()
(D) int
len()
Answer: (B) int length()
Explanation:
The `length` method in the `String` class is used to obtain the length (number of characters) of the invoking string. The method signature is `int length()`.
12) Which of
the following date class method constructs Date object using current system
time?
(A)
Systime ()
(B)
Currtime ()
(C) Date
()
(D) System
()
Answer: (C) Date()
Explanation:
13) Which
of the following refers to the starting index value in array?
(A) 0
(B) 1
(C) null
(D) All of
these
Answer: (A) 0
Explanation:
14) Which
of the following calendar class constant is used to display hour in 12 - hour
notation?
(A) HOUR_OF_DAY
(B) HOUR
(C) HOUR_OF_HALF
(D) HOUR_OF_12
Answer: (B) HOUR
Explanation:
15) Errors
can be broadly classified into how many categories?
(A) 2
(B) 3
(C) 4
(D) 5
Answer: (A) 2
Explanation:
Errors in Java can be broadly classified into 2 categories:
1. Compile-time Errors
2. Runtime Errors
16) Which
of the following Exit code indicates program is successfully compiled in Java?
(A) 1
(B) 2
(C) 3
(D) 0
Answer: (D) 0
Explanation:
17) Which
of the following Exception class is utilised when there is an attempt to divide
any number by zero?
(A) ArithmeticException
(B)
ExceptionArithmetic
(C)
ExceptionByZero
(D)
ArithmeticExpectation
Answer: (A) ArithmeticException
Explanation:
18) Which
of the following keyword is not used for an exception handler?
(A) catch
(B) cry
(C) try
(D) finally
Answer: (B) cry
Explanation:
In Java, the keywords used for exception handling are `try`, `catch`, and `finally`. The keyword `try` is used to enclose the block of code that might throw an exception. The `catch` keyword is used to catch and handle the exception. The `finally` keyword is used to specify a block of code that will be executed no matter what, whether an exception is thrown or not.
The keyword "cry" is not a standard keyword in Java for exception handling.
19) Which
of the following block must immediately follow the try block?
(A) catch
(B) cry
(C) imc
(D) final
Answer: (A) catch
Explanation:
In Java, the `catch` block immediately follows the `try` block in exception handling. The `try` block encloses a section of code where an exception might occur. If an exception occurs, the control is transferred to the `catch` block that matches the type of the thrown exception. The `catch` block contains the code to handle the exception.
The correct sequence is:
try {
// code that might throw an exception
} catch (ExceptionType e) {
// code to handle the exception
}
20) Which
Of the following block is generally used to clean up at the end of executing a
try block?
(A)
finally
(B) catch
(C) cry
(D) clean
Answer: (A) finally
Explanation:
The `finally` block in Java is generally used to clean up at the end of executing a `try` block. Regardless of whether an exception is thrown or not, the code in the `finally` block will be executed. It is typically used for releasing resources, closing files, or performing cleanup operations that need to happen irrespective of whether an exception occurs.
The correct structure is:
try {
// code that might throw an exception
} catch (ExceptionType e) {
// code to handle the exception
} finally {
// code that will be executed regardless of whether an exception occurred or not
}
21) Which of the following is an example of volatile storage Device?
(A)
optical disks
(B) RAM
(C) USB drives
(D) hard
disk
Answer: (B) RAM
Explanation:
Volatile storage devices are those that lose their data when the power is turned off. RAM (Random Access Memory) is an example of volatile storage. When the power is turned off or the system is restarted, the data stored in RAM is lost. Other examples of volatile storage include cache memory.
22) Which
of the following is an example of binary files extensions?
(A) .mp3
(B) .txt
(C) .C
(D) .java
Answer: (A) .mp3
Explanation:
23) Which
of the following class can be used to access attributes of files and directories?
(A) File
class
(B) Dir
class
(C) FD class
(D) Folder
class
Answer: (A) File class
Explanation:
24) Which
of the following class provides various methods to read, input from the
keyboard or from the file?
(A) Console
class
(B)
Scanner class
(C) Input
class
(D)
Keyboard class
Answer: (B) Scanner class
Explanation:
25) Which
of the following class provides a method for reading password?
(A) Console
class
(B)
Scanner class
(C) Input
class
(D)
Keyboard class
Answer: (A) Console class
Explanation:
26) Which
of the following package contain a collection of stream classes that supports
reading and writing in a file?
(A) Java.wr
(B)
Java.re
(C) Java.io
(D) Java.Rw
Answer: (C) Java.io
Explanation:
27) The
way of programming is divided into how many categories?
(A) Three
(B) Two
(C) Five
(D) Four
Explanation:
The way of programming is broadly divided into 2 categories:
1. Procedural Programming
2. Object-Oriented Programming (OOP)
28) Which
of the following presents a collection of classes, constraints and relationship
among classes?
(A) Root
diagram
(B) Object
diagram
(C) Class
diagram
(D) User
diagram
Answer: (C) Class diagram
Explanation:
A class diagram in object-oriented modeling shows a collection of classes, interfaces, associations, collaborations, and constraints. It represents the static view of a system and presents a blueprint of the structure of the system. Class diagrams are commonly used during the design and analysis phases of software development.
29) Which
of the following is not a visibility symbol?
(A) #
(B) *
(C) ~
(D) -
Answer: (B) *
Explanation:
In UML (Unified Modeling Language), the visibility symbols indicate the level of visibility or access control for attributes and methods in a class. The commonly used visibility symbols are:
- Public: `+` (plus sign)
- Private: `-` (minus sign)
- Protected: `#` (hash/pound sign)
- Package (default): No symbol (blank)
The symbol `*` is not used as a visibility symbol in UML; it typically doesn't represent visibility and is not part of the standard set of visibility symbols in class diagrams.
30) Which
of the following is mechanism of providing protection to data and methods of a
program?
(A)
Polymorphism
(B)
Messaging
(C)
Encapsulation
(D) Data
abstraction
Answer: (C) Encapsulation
31) Which
of the following is a concept that hides the complexity: it says what it does,
but not how it is done?
(A)
Messaging
(B)
Encapsulation
(C)
Abstraction
(D)
Polymorphism
Answer: (C) Abstraction
Explanation:
32) In
class diagram composition relationship are represented using which of the
following symbol?
(A) Filled
Diamond
(B) Filled
Circle
(C) Filled
Triangle
(D) Filled
Square
Answer: (A) Filled Diamond
Explanation:
In a class diagram, the composition relationship is represented using a filled diamond shape. The filled diamond indicates a strong relationship between two classes, where the lifetime of the contained class is dependent on the container class. It signifies a "whole-part" relationship, where the whole (container) class consists of parts (contained) class objects.
33) Which
of the following operator creates an object and returns its reference?
(A) colon
( : )
(B) new
(C) dot(.)
(D)
assignment ( = )
Answer: (B) new
Explanation:
In Java, the `new` operator is used to dynamically allocate memory for an object and create an instance of a class. It returns a reference to the newly created object. The general syntax for creating an object is:
ClassName objectName = new ClassName();
Here, `new` is the operator responsible for creating the object, and it returns a reference (`objectName`) to that object.
So, the correct answer is (B) new.
34) Which of the following represents non-exclusive relationship between two classes?
(A) Agitation
(B) Aggregation
(C)
Composition
(D)
Comparison
Answer: (B) Aggregation
Explanation:
Aggregation represents a non-exclusive relationship between two classes. It is a type of association where one class is part of another class, but there is no strict ownership, and the associated objects can exist independently. In aggregation, the whole (aggregated class) can exist without the part (aggregating class), and the part can exist independently as well.
35) What
is the full form at JDK?
(A) Java
Development Kit
(B) Java
Design Kit
(C) Java Declare
Kit
(D) Java
Derive Kit
Answer: (A) Java Development Kit
Explanation:
JDK stands for Java Development Kit. It is a software development kit used for developing Java applications. JDK includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), and other tools needed for Java development.
36) Java
supports how many primitive data types?
(A) 5
(B) 8
(C) 3
(D) 7
Answer: (B) 8
Explanation:
Java supports eight primitive data types, which are:
1. byte
2. short
3. int
4. long
5. float
6. double
7. char
8. boolean
37) Which of
the following datatype hold integer number?
(A) boolean
(B) char
(C) long
(D) double
Answer: (C) long
Explanation:
The data type "long" in Java is used to hold integer numbers, specifically for longer integer values that cannot be accommodated by the "int" data type.
38) Which of
the following escape code is used for tab in Java?
(A) \b
(B) \r
(C) \t
(D) \n
Answer: (C) \t
Explanation:
In Java, the escape code `\t` is used to represent a tab character. It is commonly used for formatting output to create horizontal space.
39) Which
of the following symbol is used for single-line comment in Java?
(A) *
(star)
(B) //
(double slash)
(C) /
(single slash)
(D) #
(hash)
Answer: (B) // (double slash)
Explanation:
In Java, the double slash `//` is used to indicate the beginning of a single-line comment. Any text following the double slash on the same line is treated as a comment and is ignored by the compiler.
40) Which of
the following symbol is used for Modulus as an Arithmetic operator in Java?
(A) *
(star)
(B) - (minus)
(C) +
(plus)
(D) % (percentage)
Answer: (D) % (percentage)
Explanation:
In Java, the percentage symbol `%` is used as the modulus operator in arithmetic operations. The modulus operator returns the remainder of the division of one number by another.
41) In java
if conditional operator is used as below
A = (N%2 ==
0)? (N/2) : (3 * N + 1);
and if N=8
then what will be A va1ue?
(A) 8
(B) 27
(C) 4
(D) 1
Answer: (C) 4
Explanation:
As per the given expression if N is even then the return would be N/2.
N=8 so answer will be (C) 4
42) Which
of the following are used to repeat a sequence of statements over and over until
some condition occurs?
(A) Loops
(B) Blocks
(C)
Variable
(D)
Cookies
Answer: (A) Loops
Explanation:
43) In switch
statement of java if no match is found then which of the following statement is
executed?
(A) case (B)
default
(C) switch (D)
break
Answer: (B) default
Explanation:
In a Java switch statement, if no match is found for any of the case values, the code under the "default" case is executed. The "default" case is optional, and it provides a block of code that is executed when none of the specified case values matches the expression's value.
44) Which
of the following loop is exit controlled loop in java?
(A) do ....
while
(B) while
(C) for
(D) for
..... while
Answer: (A) do .... while
Explanation:
45) Which
of the following allows us to build new class with added capabilities by
extending existing class?
(A) Abstraction
(B) Polymorphism
(C)
Inheritance
(D)
Messaging
Answer: (C) Inheritance
Explanation:
46) Inheritance
models which of the following relationship between two classes?
(A) is-a
(B) a-an
(C) it-was
(D) was-a
Answer: (A) is-a
Explanation:
47) In Java,
Accessor method is also known as which of the following way?
(A) setter
(B) getter
(C) Actor
(D) Acer
Answer: (B) getter
Explanation:
48) Which
of the following way, a user defined no-argument constructor is defined?
(A)
<classname>[ ] { } ;
(B)
<classname>( ) { } ;
(C)
<classname>{ } ( ) ;
(D)
<classname>[ ] ( ) ;
Answer: (B) <classname>( ) { } ;
Explanation:
A user-defined no-argument constructor in Java is defined using the following syntax:
public class ClassName {
// other class members
// No-argument constructor
public ClassName() {
// constructor body
}
// other class members
}
Here, `<classname>` is the name of the class, and the no-argument constructor has the same name as the class. This constructor is invoked when an object of the class is created without any arguments.
49) Which
of the following is not a part of four P's protection in Java?
(A)
Protected
(B)
Private
(C) Public
(D) Protocol
Answer: (D) Protocol
Explanation:
The four P's of protection in Java refer to four access modifiers used to control the visibility of classes, methods, and variables. These access modifiers are:
- Private (P): Accessible only within the same class.
- Protected (P): Accessible within the same package or by subclasses.
- Public (P): Accessible from any other class.
- Package-Private (default): Accessible within the same package.
50) Which
of the following means 'many form' in java?
(A)
Encapsulation
(B)
Abstraction
(C) Polymorphism
(D)
Messaging
Answer: (C) Polymorphism
Explanation:
In Java, "Polymorphism" means 'many forms.' It refers to the ability of a method to do different things based on the object that it is acting upon. There are two types of polymorphism in Java: compile-time polymorphism (method overloading) and runtime polymorphism (method overriding). Polymorphism allows objects of different classes to be treated as objects of a common base class, providing flexibility and extensibility in code design.