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:

In a class diagram, visibility modifiers are represented by specific symbols. The symbol '#' (hash) represents package visibility. Package visibility means that the member (e.g., attribute or method) is accessible within the same package but not outside the package.


 

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:

UML stands for Unified Modelling Language. It is a standardized general-purpose modelling language in the field of software engineering. UML provides a set of graphic notation techniques to create visual models of object-oriented software systems.


 

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:

In Aggregation, a non-exclusive relationship is shared between two classes. Aggregation represents a "whole-part" relationship where a part can exist independently of the whole. It is a weaker form of association where the child can exist independently of the parent. The non-exclusive relationship implies that the existence of the part is not dependent on the existence of the whole.


 

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.


After this operation, the value of `x` will be incremented to 6.


 

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:

Documentation comments in Java are used for creating API documentation from the code. These comments start with `/**` and can be used to generate documentation using tools like Javadoc. They allow developers to provide information about classes, methods, and fields, including descriptions, parameter details, return values, etc. Documentation comments help in generating comprehensive documentation for the codebase.


 

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:

The 'short' data type in Java is a 16-bit signed integer. It can represent values in the range from -32768 to 32767.


 

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:

When a Java program is successfully compiled without errors, the Java compiler generates bytecode, and the compiled bytecode is stored in a file with a .class extension. The .class file contains the compiled code that can be executed by the Java Virtual Machine (JVM).


 

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:

Instance methods define the behavior of an object. These methods are associated with an instance of a class, and they operate on the instance variables of the class. Instance methods are called on an object, and they can access and modify the state of that object. They are defined within the class and are invoked using an instance of the class.


 

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:

The garbage collector in Java is responsible for identifying and reclaiming memory that is occupied by objects that are no longer in use. It automatically frees up memory by deallocating objects that are no longer reachable or referenced by the program. This process helps manage memory efficiently and prevents memory leaks in Java programs.


 

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:

Class methods in Java are methods that are associated with the class rather than an instance of the class. They are defined using the `static` keyword and are accessible at the class level, not requiring an instance of the class to be created. Class methods can be called using the class name and are often used for operations that are not dependent on the specific instance of the class.


 

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

    }

}


So, option (D) <classname> ( ) { }; represents the correct syntax for a user-defined no-argument constructor.


 

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.


So, the correct answer is (B) Private.


 

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.


So, the correct answer is (C) new.


 

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.


So, the correct answer is (D) constructors.


 

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.


So, the correct answer is (B) methods.


 

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> [];


So, the correct option is (C) <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.


So, the correct option is (D) Binary Search ( ).


 

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:

In Java, the `length` property is used to determine the size or length of an array. For a one-dimensional array, it provides the number of elements in the array. So, the correct option is (A) length.


 

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:

The correct method to return the character at a specific index position from the invoking string is `char charAt(int index)`. Therefore, the correct option is (C) char charAt(int index).


 

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:

The `getTime()` method of the `Date` class in Java returns the number of milliseconds since January 1, 1970, GMT. Therefore, the correct option is (C) long getTime ( ).


 

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:

The `HOUR_OF_DAY` constant of the `Calendar` class in Java represents the hour of the day for the 24-hour clock and is used to display the hour in 24-hour notation. Therefore, the correct option is (D) HOUR_OF_DAY.


 

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


Therefore, the correct option is (B) 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.


Therefore, the correct option is (C) `fill(list, 7)`.


 

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.


Therefore, the correct option is (B) Exception.


 

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`.


Therefore, the correct option is (A) 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

Answer: (A) finally


 

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.


So, if you want to inform that the code within a method or constructor may throw an exception, you use the `throws` clause in the method or constructor signature.


 

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

     }

 Answer: (D) try

     {

            // 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:

The `listFiles()` method of the `java.io.File` class in Java returns an array of abstract pathnames denoting the files in the directory. This method is used to obtain the list of files in the current directory. The returned array contains `File` objects representing the files in the directory.


 

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:

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 provide a common I/O model for reading from or writing to various sources, such as files, byte arrays, network connections, etc. There are two types of streams: input streams and output streams. They provide a uniform way to interact with different types of data sources and destinations in a consistent manner.


 

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.

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