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

1) Which of the following is provided using encapsulation?

(A) Data protection

(B) Data sharing

(C) Separation of data and methods

(D) All of the above

Answer: (A) Data protection

Explanation:

Encapsulation in object-oriented programming provides:


- Data protection: Encapsulation allows the hiding of the implementation details of a class and only exposing the necessary functionalities. This helps in protecting the data from unauthorized access and modification.



2) With which of the following options polymorphism cannot be achieved?

(A) Method Overloading

(B) Operator Overloading

(C) Data hiding

(D) All of the above

Answer: (C) Data hiding

Explanation:

Polymorphism can be achieved through method overloading and operator overloading. These concepts allow multiple methods or operators to have the same name but different implementations, providing a way to achieve polymorphism.


- Method Overloading: In method overloading, multiple methods in the same class have the same name but different parameters.


- Operator Overloading: Operator overloading allows the use of the same operator for different types or classes, with the behavior of the operator determined by the types involved.


On the other hand, data hiding (encapsulation) is not a technique for achieving polymorphism. Data hiding focuses on hiding the implementation details of a class and protecting its internal state.


So, the correct answer is (C) Data hiding.


 

3) An inheritance model refers to which of the following relationships?

(A) ‘is-a’

(B) 'has-a'

(C) 'a-part-of’

(D) all of these

Answer:  (A) ‘is-a’

Explanation:

An inheritance relationship in object-oriented programming is often described using the "is-a" relationship. Inheritance represents an "is-a" relationship between classes, where a subclass inherits the characteristics (attributes and behaviors) of a superclass. For example, if you have a class `Dog` and a class `Labrador` that extends `Dog`, you can say that a `Labrador` "is-a" `Dog`.


The other relationships mentioned, such as "has-a" and "a-part-of," are associated with composition or aggregation, not inheritance.


- "Has-a" relationship is related to composition, where one class has another class as a component.

- "A-part-of" is not a standard term in object-oriented programming.


So, the correct answer is (A) ‘is-a’.



4) Which Of the following describes a group of objects with similar attributes and common behavior?

(A) Object

(B) Class

(C) Program

(D) Diagram

Answer: (B) Class

Explanation:

In object-oriented programming, a class is a blueprint or template that defines a group of objects with similar attributes and common behavior. Objects are instances of a class, and they encapsulate data (attributes) and behavior (methods). A class serves as a blueprint for creating multiple objects that share the same structure and behavior.


So, the correct answer is (B) Class.


 

5) Which of the following is a mechanism of providing protection to data and methods of a program?

(A) Encapsulation

(B) Data abstraction

(C) Polymorphism

(D) Aggregation

Answer: (A) Encapsulation

Explanation:

Encapsulation is the mechanism of providing protection to data and methods by bundling them together into a single unit, typically a class. It involves hiding the internal state of an object and restricting access to the internal details. This helps in preventing unauthorized access and modification of the data, promoting data integrity and security.


So, the correct answer is (A) Encapsulation.


 

6) Which Of the following represents non-exclusive relationship between two classes?

(A) Aggregation

(B) Composition

(C) Inheritance

(D) All of the above

Answer: (A) Aggregation

Explanation:

Aggregation is a non-exclusive relationship between two classes, where one class contains another class as a part, but the part can exist independently of the whole. It represents a "has-a" relationship without strong ownership or dependency. In aggregation, the relationship is often described as a weaker form of association.


In contrast, composition represents a stronger form of ownership or dependency, and inheritance represents an "is-a" relationship.


So, the correct answer is (A) Aggregation.


 

7) Which of the following is used to distinguish objects from each other?

(A) Attributes

(B) State

(C) Behavior

(D) All of the above

Answer: (B) State

Explanation:

Attributes are used to distinguish objects from each other. In object-oriented programming, an object's attributes represent its properties or characteristics. These attributes differentiate one object from another by storing specific information about each object.


While state and behavior are important concepts in object-oriented programming:


- State: The state of an object is represented by its attributes. It reflects the current values of the object's properties.


- Behavior: The behavior of an object is represented by its methods. Methods define what actions or operations the object can perform.


So, the correct answer is  (B) State



8) In object-oriented terminology, with reference to Inheritance, existing class is also called which of the following?

(A) super class

(B) parent class

(C) base class

(D) all of the above

Answer: (D) all of the above

Explanation:

In object-oriented terminology, the existing class in the context of inheritance is also referred to by various terms:


- Super class: It is called the super class because it serves as the parent class for the classes that inherit from it.


- Parent class: The term "parent class" emphasizes its role as the class from which other classes are derived.


- Base class: Similarly, it is called the base class because it is the starting point for the inheritance hierarchy.


All three terms (super class, parent class, base class) are commonly used and refer to the same concept in the context of inheritance.


So, the correct answer is (D) all of the above.



9) How many primitive data types are supported in Java?

(A) 2

(B) 4

(C) 8

(D) 16

Answer: (C) 8

Explanation:

Java supports 8 primitive data types:


1. byte: 8-bit integer

2. short: 16-bit integer

3. int: 32-bit integer

4. long: 64-bit integer

5. float: 32-bit floating-point

6. double: 64-bit floating-point

7. char: 16-bit Unicode character

8. boolean: Represents true or false values


So, the correct answer is (C) 8.

 


10) Java language was developed by which of the following company?

(A) Sun Microsystems

(B) Netscape

(C) Microsoft

(D) Oracle

Answer: (A) Sun Microsystems

Explanation:

Java was originally developed by Sun Microsystems. It was initiated by James Gosling and Mike Sheridan at Sun Microsystems in the early 1990s. Later, Java technology was acquired by Oracle Corporation when Oracle acquired Sun Microsystems in 2010.


So, the correct answer is (A) Sun Microsystems.


 

11) Which of the following extension file is generated by compiler when the program gets compiled without errors in Java?

(A) .java

(B) .C

(C) .org

(D) .class

Answer: (D) .class

Explanation:

In Java, when a program is compiled without errors, the Java compiler generates bytecode, and the bytecode is stored in a file with a ".class" extension. This bytecode is platform-independent and can be executed by the Java Virtual Machine (JVM).


So, the correct answer is (D) .class.



12) Single-line comment begins with which sign in Java?

(A) /*

(B) //

(C) /**

(D) All of the above

Answer: (B) //

Explanation:

In Java, a single-line comment begins with the "//" (double forward slash) sign. Anything following the "//" on the same line is treated as a comment and is ignored by the compiler.


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



13) What will be the result of arithmetic expression 9 % 2?

(A) 4.5

(B) 1

(C) 4

(D) 0

Answer: (B) 1

Explanation:

The expression "9 % 2" represents the remainder when 9 is divided by 2 using the modulo operator (%). In this case, the remainder is 1 because 9 divided by 2 equals 4 with a remainder of 1.


So, the correct answer is (B) 1.



14) What is it called when ++ operator is used after variable name in Java?

(A) post increment

(B) pre increment

(C) increment pre

(D) pre decrement

Answer: (A) post increment

Explanation:

In Java, when the "++" operator is used after the variable name (e.g., `variable++`), it is called post-increment. The current value of the variable is used in the expression, and then the variable is incremented.


So, the correct answer is (A) post increment.



15) Which of the following is used to repeat a sequence of statements over and over until some condition occurs?

(A) block

(B) array

(C) loops

(D) boolean

Answer: (C) loops

Explanation:

Loops are used to repeat a sequence of statements over and over until some condition occurs. In Java, there are different types of loops, such as the "for" loop, "while" loop, and "do-while" loop, which allow you to iterate through a block of code based on a specified condition.


So, the correct answer is (C) loops.



16) Which statement is executed if the value of boolean expression is true?

 

            if (boolean expression)

            {

                        <statement1>

            }

            else

            {

                <statement2>

            }

            statement3

 

(A) statement2

(B) error message

(C) statement1

(D) statement3

Answer: (C) statement1

Explanation:

If the value of the boolean expression is true, then `<statement1>` will be executed, and `statement3` will be executed after the if-else block. `<statement2>` will be skipped because the "else" part is executed only when the boolean expression is false. 



17) Which of the following statement is used to transfer the control outside 'Switch' or ‘Loop' construct?

(A) continue

(B) else

(C) end

(D) break

Answer:  (D) break

Explanation:

The "break" statement is used to terminate the execution of a loop or switch statement. It transfers the control outside of the loop or switch construct. In the context of loops, it is commonly used to exit a loop prematurely based on a certain condition. In the context of a switch statement, it is used to exit the switch construct after a case is matched.


So, the correct answer is (D) break.



18) Which of the following variable name is not valid in Java?

(A) _marks

(B) 4subject

(C) total_marks

(D) $age

Answer:  (B) 4subject

Explanation:

In Java, variable names must follow certain rules:


1. Variable names must start with a letter, underscore (_), or dollar sign ($).

2. After the first character, variable names can also contain digits (0-9).


Given the options:


- (A) _marks is valid because it starts with an underscore.

- (B) 4subject is not valid because it starts with a digit, which is not allowed.

- (C) total_marks is valid because it starts with a letter.

- (D) $age is valid because it starts with a dollar sign.


So, the correct answer is (B) 4subject.



19) Which of the following defines attributes and methods?

(A) class

(B) object

(C) instance

(D) variable

Answer: (A) class

Explanation:

In object-oriented programming, a class is used to define a blueprint for objects. It defines attributes (data members) and methods (functions) that the objects of the class will have. An object is an instance of a class, and variables within a class are often referred to as attributes.


So, the correct answer is (A) class.


 

20) Which keyword is used in Java to define a class?

(A) DEFINE

(B) define

(C) class

(D) Define

Answer: (C) class

Explanation:

In Java, the keyword used to define a class is "class". So, the correct answer is (C) class.


 

21) Which step(s) are required to create an object from the class?

(A) Declaration

(B) Instantiation

(C) Initialization

(D) All of the above

Answer: (D) All of the above

Explanation:

To create an object from a class in Java, you need to perform the following steps:


1. Declaration: Declare a variable of the class type. For example, `ClassName objectName;`

2. Instantiation: Use the `new` keyword to create an instance of the class. For example, `objectName = new ClassName();`

3. Initialization: Set initial values to the attributes or call methods to initialize the object.


So, all of the above steps (Declaration, Instantiation, and Initialization) are required to create an object from a class.



22) Which keyword is used to create an object by allocating memory?

(A) memory

(B) new

(C) allocate

(D) variable

Answer: (B) new

Explanation:

In Java, the "new" keyword is used to dynamically allocate memory for an object. It is used along with the class constructor to create a new instance of the class. For example:


ClassName objectName = new ClassName();


Here, `new ClassName()` allocates memory for an object of the class `ClassName`.


 

23) By which of the following operator, instance variables and instance methods are accessed via objects?

(A) ; operator

(B) . operator

(C) * operator

(D) % operator

Answer: (B) . operator

Explanation:

In Java, the dot (`.`) operator is used to access instance variables and invoke instance methods via objects. For example:


ClassName object = new ClassName();

int value = object.variableName; // accessing instance variable

object.methodName(); // invoking instance method


So, the correct answer is (B) . operator.


 

24) Which of the following statement is used to declare class variable totWindows in class with instance variables?

(A) static int totWindows;

(B) Static Int totWindows;

(C) STATIC totWindows;

(D) Int totWindows•,

Answer: (A) static int totWindows;

Explanation:

To declare a class variable in Java, you use the `static` keyword. The correct syntax is:


static dataType variableName;


So, the correct answer is (A) static int totWindows;


 

25) Which variables are not initialized by default values in Java?

(A) Instance variables

(B) Class variables

(C) Local variables

(D) All of the above

Answer: (C) Local variables

Explanation:

In Java, instance variables and class variables are automatically initialized to default values if no explicit value is assigned. However, local variables must be explicitly initialized before they are used, as they do not have default values. So, the correct answer is (C) Local variables.



26) In java, which is a special kind of method that is invoked when new object is created?

(A) Modifiers

(B) Polymorphism

(C) Class

(D) Constructor

Answer: (D) Constructor

Explanation:

In Java, a constructor is a special method that is automatically invoked when a new object is created. It is used to initialize the object and allocate resources. Constructors have the same name as the class and do not have a return type.


So, the correct answer is (D) Constructor.



27) Which access modifier in Java provides visibility to classes defined in other package also?

(A) Private

(B) Public

(C) Protected

(D) All of the above

Answer: (B) Public

Explanation:

The "public" access modifier in Java provides the highest level of visibility. If a class, method, or variable is declared as public, it can be accessed from any other class, including those defined in other packages.


So, the correct answer is (B) Public.


 

28) Which of the following keyword is used to refer a superclass constructor in subclass constructor?

(A) extends

(B) super

(C) name of the superclass

(D) new

Answer: (B) super

Explanation:

In Java, the "super" keyword is used to refer to the superclass constructor in the subclass constructor. It is used to call the constructor of the immediate parent class. The syntax is:


super(parameters);


So, the correct answer is (B) super.


 

29) Which is a variable in Java representing a collection of homogeneous type of elements?

(A) array

(B) function

(C) for loop

(D) while loop

Answer: (A) array

Explanation:

In Java, an array is a variable that represents a collection of homogeneous (of the same type) elements. It is a data structure that stores elements of the same data type in contiguous memory locations.


So, the correct answer is (A) array.


 

30) Which of the following syntax is used to declare array in Java?

(A) <data type> <array name> [];

(B) <data type> [] <array name>;

(C) Both (A) and (B)

(D) None of the above

Answer: (C) Both (A) and (B)

Explanation:

In Java, you can declare an array using either of the following syntaxes:


(A) `<data type> <array name>[];`


(B) `<data type>[] <array name>;`


Both syntaxes are valid, and you can choose the one that you find more readable or convenient.


So, the correct answer is (C) Both (A) and (B).


 

31) How many bytes of memory location are occupied by an array 'b' of int b [][] = new int [5][3];?

(A) 15

(B) 60

(C) 5

(D) 3

Answer: (B) 60

Explanation:

In Java, a multidimensional array is essentially an array of arrays. In the case of the given array declaration:


int b[][] = new int[5][3];


The array 'b' is a 2D array with 5 rows and 3 columns. The total number of elements in the array is 5 * 3 = 15. Each element is of type 'int', which typically occupies 4 bytes of memory. Therefore, the total memory occupied by the array 'b' is 15 * 4 = 60 bytes.


So, the correct answer is (B) 60.


 

32) What can be considered as a sequence of characters in Java?

(A) group

(B) null value

(C) string

(D) object

Answer: (C) string

Explanation:

In Java, a sequence of characters is represented by the `String` class. Therefore, the correct answer is (C) string. A string is a data type that is used to represent text or a sequence of characters. Strings in Java are objects of the `String` class and can be created using double quotes, like `"Hello, World!"`.


 

33) Which of the following statement is true due to compare the contents of str1 and str2?

(A) str1 = str2

(B) str1 != str2

(C) str1 (=) str2

(D) str1 = = str2

Answer: (B) str1 != str2

Explanation:

In Java, to compare the contents of two strings, you should use the `equals` method. The `!=` operator is used to check if two references point to different objects in memory, not if the contents are the same. Therefore, the correct way to compare the contents of `str1` and `str2` is to use `!str1.equals(str2)`, which means "not equal." So, the correct answer is (B) `str1 != str2`.


 

34) For which of the tasks, methods are provided by string class in Java?

(A) Compare strings

(B) Find length of string

(C) Combining strings

(D) All of the above

Answer: (D) All of the above

Explanation:

The `String` class in Java provides methods for various tasks, including:


(A) Compare strings: The `equals` method is used to compare the contents of two strings.

(B) Find length of string: The `length` method returns the number of characters in a string.

(C) Combining strings: The `concat` method or the `+` operator can be used to concatenate (combine) strings.


So, all of the options (A), (B), and (C) are tasks for which methods are provided by the `String` class.


 

35) Which of the following statement is true to return a string with all characters of str1 converted to lower case?

(A) str1.toLowerCase ( )

(B) str1 .Tolowercase ( )

(C) str1.TOLOWERCASE()

(D) toLowerCASE()

Answer: (A) str1.toLowerCase()

Explanation:

The correct method to return a string with all characters of `str1` converted to lower case in Java is `str1.toLowerCase()`. Java is case-sensitive, so it's important to use the correct syntax. The `toLowerCase()` method is used to convert all the characters in a string to lowercase.


 

36) Date class is available in which of the following Java package?

(A) java.Date package

(B) java.util package

(C) java.All package

(D) java.Calendar package

Answer: (B) java.util package

Explanation:

The `Date` class in Java is available in the `java.util` package. This class represents a specific instant in time, with millisecond precision.



37) Which information can be extracted by Calendar class of java.util package of Java?

(A) Year, Month

(B) Hour, Minute

(C) (A) & (B) both

(D) None of the above

Answer: (C) (A) & (B) both

Explanation:

The `Calendar` class in the `java.util` package of Java provides information about the year, month, hour, and minute, among other date and time components. Therefore, both (A) Year, Month and (B) Hour, Minute can be extracted using the `Calendar` class.


 

38) What does 'length' refer to for an object of String class?

(A) attribute

(B) method

(C) class variable

(D) class name

Answer: (B) method

Explanation:

In the context of a String object in Java, `length` refers to a method. The `length()` method of the String class is used to obtain the length of the string, i.e., the number of characters in the string.


 

39) Which of the following refers to an error condition in object-oriented programming terminology?

(A) anomaly

(B) abbreviation

(C) exception

(D) deviation

Answer: (C) exception

Explanation:

In object-oriented programming terminology, the term "exception" is used to refer to an error condition. Exceptions are events that occur during the execution of a program that disrupt the normal flow of the program's instructions. Handling exceptions is an essential part of writing robust and error-tolerant programs.


 

40) Which of the following block handles or takes appropriate action when an Exception occurs?

(A) try

(B) catch

(C) throws

(D) handles

Answer: (B) catch

Explanation:

The "catch" block is used to handle or take appropriate action when an exception occurs in Java. The try-catch statement allows you to write code that may throw exceptions within the "try" block, and the corresponding "catch" block is used to catch and handle those exceptions.


 

41) Which Of the following is an advantage of using a try catch block?

(A) Exceptional events are eliminated

(B) Exceptional events are reduced

(C) Exceptional events are integrated with regular events

(D) Exceptional events are isolated from 'regular events

Answer: (D) Exceptional events are isolated from 'regular events'

Explanation:

The primary advantage of using a try-catch block is that it allows you to isolate and handle exceptional events separately from regular events. In a try-catch block, you can write code that may throw exceptions within the "try" block, and the corresponding "catch" block is used to catch and handle those exceptions. This isolation helps in managing and handling exceptional situations without affecting the regular flow of the program.

 


42) Which exit code in SciTE editor indicates that the command execution is successful?

(A) 0

(B) 1

(C) -1

(D) 10

Answer: (A) 0

Explanation:

In SciTE editor, the exit code indicating successful command execution is typically 0.



43) Which clause signifies that there is no catch block within the method that can handle the exception?

(A) throws

(B) if

(C) loop

(D) array

Answer: (A) throws

Explanation:

In Java, the "throws" clause is used to declare that a method might throw one or more types of exceptions. It is part of the method signature and indicates the exceptions that the method might propagate to the caller. When a method contains a "throws" clause, it means that the method does not handle the exceptions itself but instead, it allows the exceptions to be propagated to the calling method or to the runtime system.


The "if," "loop," and "array" are not related to specifying how exceptions are handled in Java. The "throws" clause is specifically used for declaring exceptions that a method may throw, and it does not provide a mechanism for handling those exceptions within the method itself.

 


44) Which class in Java is used to accept input from the Keyboard?

(A) java.keyboard

(B) java.util.Input

(C) java.SCANNER

(D) java.util.Scanner

Answer: (D) java.util.Scanner

Explanation:

In Java, the `Scanner` class, which is part of the `java.util` package, is commonly used to accept input from the keyboard. It provides methods to read different types of input, such as integers, doubles, strings, etc., from various input sources, including the standard input (keyboard). The `Scanner` class makes it convenient to interact with user input in console-based Java applications.



45) In which of the following storage type, values are stored in variables are lost when a computer is shutdown?

(A) Volatile Storage

(B) Non-Volatile Storage

(C) Both (A) and (B)

(D) None of the above

Answer: (A) Volatile Storage

Explanation:

Volatile storage refers to the type of storage where values stored in variables are lost when the power is turned off or the computer is shutdown. RAM (Random Access Memory) is a common example of volatile storage. In contrast, non-volatile storage retains its data even when the power is turned off, and examples include hard drives, SSDs, and flash memory.


 

46) How many methods are available in Java for a file class that can be used to perform various operations on a file?

(A) 3

(B) 120

(C) 8

(D) 30

Answer: (D) 30


47) In Linux, "passwd" file present in which directory stores the information of the users existing in the system?

(A) /root

(B) /extra

(C) /etc

(D) None of the above

Answer:  (C) /etc

Explanation:

In Linux, the "/etc" directory is a system-wide configuration directory that contains various configuration files, including the "passwd" file. The "/etc/passwd" file stores information about user accounts on the system, such as usernames, user IDs, group IDs, home directories, and shell information. It is a crucial file for user account management in Unix-like operating systems.



48) Which is an abstract representation of an input or output device that is used as a source or destination for data?

(A) stream

(B) scanner

(C) method

(D) function

Answer: (A) stream

Explanation:

In programming, 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 convenient way to handle input and output operations, allowing data to flow between a program and external sources or sinks. Streams can be associated with various devices, such as files, network sockets, or the console, and they facilitate the reading and writing of data in a sequential manner.


 

49) Which of the following format is used in Java to store a character?

(A) ASCII

(B) Unicode

(C) Both (A) & (B)

(D) None of the above

Answer: (B) Unicode

Explanation:

In Java, characters are stored using Unicode. Unicode is a character encoding standard that represents almost all of the world's written languages. 


 

50) Which class defines the functionality that is available for all character input streams?

(A) void reader class

(B) Exact reader class

(C) abstract reader class

(D) super reader class

Answer: (C) abstract reader class

Explanation:

In Java, the abstract class `Reader` is the superclass of all classes representing character input streams. It defines the basic functionality that is available for all character input streams. Subclasses of `Reader` provide specific implementations for reading characters from various sources, such as files, strings, or network connections. The `Reader` class is part of the `java.io` package.



Popular posts from this blog

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

TRIO Tutor Classes Ankleshwar