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:

In Java, the access modifiers determine the visibility or accessibility of classes, methods, and fields. The highest level of protection is achieved with the `private` access modifier. When a class member (method or field) is declared as `private`, it is only accessible within the same class. This means that other classes cannot access or modify the `private` members directly. Using `private` provides a high level of encapsulation and helps in controlling access to the internal details of a class.


 

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:

In Java, a string is a sequence of characters. It is represented using the `String` class and is used to store and manipulate text. The other options represent different data types: `boolean` is a data type for true/false values, `char` is a data type for single characters, and `int` is a data type for integers.


 

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:

The `equals` method in the `String` class is used to compare the contents of two strings. It returns `true` if the invoking string is equal to the specified string (`str`) and `false` otherwise. The method signature is `boolean equals(String str)`.


 

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:

The `Date()` constructor of the `Date` class in Java constructs a `Date` object representing the current date and time based on the system clock.


 

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:

In Java and many programming languages, array indices typically start from 0. The first element of an array is accessed using the index 0.


 

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:

The `HOUR` constant in the `Calendar` class is used to represent the hour of the day in 12-hour notation. It is a field that can be used to get or set the hour component of a `Calendar` object in the 12-hour format.


 

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:

In Java, an exit code of 0 indicates that the program has successfully executed without any errors. Exit codes are used to convey the status of a program's execution. Typically, a value of 0 is used to indicate success, and non-zero values are used to indicate various error conditions.


 

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:

In Java, the ArithmeticException class is used to represent an exceptional condition that occurs when an arithmetic operation is attempted, but the result is not a valid number. One common scenario is division by zero, which leads to an ArithmeticException. Therefore, when there is an attempt to divide any number by zero, an ArithmeticException is thrown.


 

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.


In contrast, non-volatile storage devices (option D: hard disk) retain their data even when the power is turned off.


 

22) Which of the following is an example of binary files extensions?

(A) .mp3

(B) .txt

(C) .C

(D) .java

Answer: (A) .mp3


Explanation:

Binary files contain data in a format that is not human-readable, unlike text files. The .mp3 extension is commonly associated with binary files, specifically audio files encoded in the MP3 format. MP3 files store compressed audio data and are not human-readable as plain text. On the other hand, .txt, .C, and .java extensions are typically associated with text files and source code files, which contain human-readable text.


 

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:

The File class in Java is used to represent and manipulate attributes of files and directories. It provides methods for creating, deleting, and examining files and directories. The File class is part of the java.io package and is commonly used for file I/O operations in Java programs.


 

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:

The Scanner class in Java provides various methods for reading input from different sources, including the keyboard and files. It is commonly used for parsing input and can tokenize and parse data in various formats. The Scanner class is part of the java.util package and is widely used for handling input in Java programs.


 

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:

The Console class in Java provides a method for reading passwords without echoing the characters to the console. The `readPassword` method of the Console class is specifically designed for securely reading passwords, and it is commonly used in scenarios where sensitive information needs to be entered without revealing it on the screen.


 

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:

The `java.io` package in Java contains a collection of classes for handling input and output operations, including reading and writing to files. The classes in this package provide various streams and readers/writers that facilitate file operations in Java.


 

27) The way of programming is divided into how many categories?

(A) Three

(B) Two

(C) Five

(D) Four

 Answer: (B) Two


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:

Abstraction is a concept in object-oriented programming that involves hiding the complex implementation details and showing only the necessary features of an object. It allows developers to focus on what an object does without having to understand how it achieves its functionality internally. Abstraction helps in simplifying the system by breaking it into smaller, manageable parts and providing a clear separation between the object's interface and its implementation.


 

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.


So, the correct answer is (A) Filled Diamond.


 

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.


So, the correct answer is (B) Aggregation.


 

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.


So, the correct answer is (A) Java Development Kit.


 

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


So, the correct answer is (B) 8.


 

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.


So, the correct answer is (C) long.


 

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.


So, the correct answer is (C) \t.


 

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.


So, the correct answer is (B) // (double slash).


 

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.


So, the correct answer is (D) % (percentage).


 

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:

Loops are used to repeat a sequence of statements over and over until some condition occurs. They are an essential part of programming and allow for the efficient execution of repetitive tasks. There are different types of loops in programming, such as for loop, while loop, and do-while loop, each serving specific purposes in controlling the flow of a program.


 

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:

In Java, the do...while loop is an exit-controlled loop. In an exit-controlled loop, the condition is checked after the execution of the loop body. This means that the loop body will always execute at least once, even if the condition is false initially.


 

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:

Inheritance in object-oriented programming allows us to build a new class with added or modified capabilities by extending an existing class. The new class, known as the subclass or derived class, inherits the properties and behaviors of the existing class, known as the superclass or base class. This promotes code reuse and facilitates the creation of a hierarchy of classes.


 

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:

Inheritance models an "is-a" relationship between two classes. It implies that a subclass is a specialized version of its superclass, inheriting its properties and behaviors. For example, if we have a class "Dog" and a subclass "Labrador," we can say that a Labrador is a type of Dog. This relationship is expressed using the "extends" keyword in Java.


 

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:

In Java, an accessor method is commonly known as a "getter" method. It is a method that is used to retrieve the value of an instance variable (property) of a class. The naming convention for getter methods is typically "getPropertyName" or "isPropertyName" for boolean properties. Getter methods provide a way to access the state of an object while encapsulating the implementation details.


 

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.


"Protocol" is not a part of the four P's of protection in Java. The correct options are Private, Protected, and Public.



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.

Popular posts from this blog

Gujarat Board, Class XII, Computer Science, Year 2018, Java Questions with Answers and Explanation

TRIO Tutor Classes Ankleshwar

Gujarat Board, Class XII, Computer Science, Year 2020, Java Questions with Answers and Explanation