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

1) Object oriented programming allows defining more than one method having same name but different signatures in a single class is known as which of the following?

(A) Method overloading

(B) Message

(C) Aggregation

(D) Composition

Answer: (A) Method overloading

Explanation:

Object-oriented programming allows defining more than one method with the same name but different parameter lists (signatures) in a single class. This feature is known as method overloading.


Method overloading enables a class to have multiple methods with the same name but different parameter types or a different number of parameters. The compiler determines which method to call based on the arguments provided during the method invocation.


So, the correct answer is (A) Method overloading.



2) Which of the following is known as objects of one class are composed of objects of other class?

(A) Method overloading

(B) Message

(C) Aggregation or Composition

(D) Abstraction

Answer: (C) Aggregation or Composition

Explanation:

The concept where objects of one class are composed of objects of another class is known as Aggregation or Composition. Aggregation and Composition are two forms of association in object-oriented programming:


1. Aggregation: It represents a "has-a" relationship, where one class has a reference to another class but doesn't own the other class. It is a weaker relationship, and the objects can exist independently. In aggregation, the part (object) can exist independently of the whole.


2. Composition: It also represents a "has-a" relationship, but in this case, one class owns the other class. The lifetime of the part (object) is dependent on the lifetime of the whole. If the whole is destroyed, the parts are also destroyed.


So, the correct answer is (C) Aggregation or Composition. 



3) Which of the following represent non-exclusive relationship between two classes?

(A) Comparison

(B) Composition

(C) Aggregation

(D) Abstraction

Answer: (C) Aggregation

Explanation:

Aggregation represents a non-exclusive relationship between two classes. In aggregation, one class is part of another class, but the relationship is more relaxed than composition. The objects can exist independently of each other, and there is no strict ownership.


On the other hand:


- Composition represents an exclusive relationship where one class owns the other class. The objects in a composition relationship cannot exist independently, and the lifetime of the part is dependent on the lifetime of the whole.


So, the correct answer is (C) Aggregation.



4) An Inheritance model refers to which of the following relationships?

(A) 'is-a-kind-of'

(B) 'has-a'

(C) 'a-part-of'

(D) 'of-a'

Answer: (A) 'is-a-kind-of'

Explanation:

An inheritance model in object-oriented programming represents an "is-a-kind-of" relationship. Inheritance is a mechanism that allows a class (subclass or derived class) to inherit properties and behaviors from another class (superclass or base class).


For example, if you have a class `Bird` and another class `Parrot`, and `Parrot` inherits from `Bird`, you can say that a `Parrot` is a kind of `Bird`.


So, the correct answer is (A) 'is-a-kind-of'.



5) In Class diagram, Composition is represented using, which of the following symbol

(A) Empty diamond symbol

(B) Filled diamond symbol

(C) Empty triangle symbol

(D) All of these

Answer: (B) Filled diamond symbol

Explanation:

In a class diagram, Composition is represented using a filled diamond symbol. The filled diamond indicates a strong ownership relationship where the existence of the "whole" class (containing the diamond) is responsible for the existence of the "part" class (connected by the diamond). If the whole is deleted, the parts are also deleted.


So, the correct answer is (B) Filled diamond symbol.



6) Which of the following can be used to create models of Object-oriented software to help with design of an application?

(A) Unified Modelling Language

(B) Unified Markup Language

(C) Unified Makeup Language

(D) Unified Morphing Language

Answer: (A) Unified Modelling Language

Explanation:

Unified Modeling Language (UML) can be used to create models of object-oriented software to help with the design of an application. UML is a standardized modeling language used in software engineering for visualizing, specifying, constructing, and documenting the artifacts of a software system.


UML provides a set of graphical notations for creating visual models of software systems, including class diagrams, use case diagrams, sequence diagrams, and more. It is widely used in the software development industry for visualizing and documenting the design of complex systems.


So, the correct answer is (A) Unified Modelling Language.



7) Which of the following is not a visibility symbol?

(A) ~ (Tilde)

(B) * (Star)

(C) # (Hash)

(D) - (Dash)

Answer: (B) * (Star)

Explanation:

In the context of UML (Unified Modeling Language) class diagrams, the symbols ~ (Tilde), # (Hash), and - (Dash) are used as visibility symbols, indicating the visibility or access level of class members. These symbols represent:


- ~ (Tilde): Package or default visibility

- # (Hash): Protected visibility

- - (Dash): Private visibility


However, * (Star) is not a standard UML visibility symbol. It is not used to represent visibility in UML class diagrams.


So, the correct answer is (B) * (Star).



8) Which of the following is a concept that hides the complexity, it says what it does, but not how it is done?

(A) Message

(B) Polymorphism

(C) Abstraction

(D) Data

Answer: (C) Abstraction

Explanation:

Abstraction is the concept that hides the complexity of a system by focusing on the essential features and ignoring the details. It allows you to represent the essential features of an object while suppressing the unnecessary details. In other words, abstraction provides a way to provide a simple interface or representation that highlights the functionality of an object and hides the implementation details.


So, the correct answer is (C) Abstraction.



9) Where Java language developed?

(A) SON Microsystems

(B) Apple Systems

(C) Oracle Systems

(D) Sun Microsystems

Answer: (D) Sun Microsystems

Explanation:

Java was originally developed by Sun Microsystems. The project started in 1991, and the programming language was officially released by Sun Microsystems in 1995. The key architects of Java were James Gosling, Mike Sheridan, and Patrick Naughton. The language was designed to be platform-independent and to provide a secure and portable way to develop software.


Note: Oracle Corporation acquired Sun Microsystems in 2010, so the current steward of the Java platform is Oracle.


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



10) What is the full form of JVM?

(A) Java Virtual Machine

(B) Java Vertical Machine

(C) Java Varity Machine

(D) Java Visual Machine

Answer: (A) Java Virtual Machine

Explanation:

The full form of JVM is "Java Virtual Machine." JVM is a virtual machine that enables a computer to run Java programs and provides a runtime environment for executing Java bytecode. It abstracts the underlying hardware and operating system, allowing Java programs to be executed in a platform-independent manner.


So, the correct answer is (A) Java Virtual Machine.



11) Which of the following editor is used to create Java file in Ubuntu?

(A) Writer

(B) Calc

(C) SciTE

(D) WordPad

Answer: (C) SciTE

Explanation:

SciTE (Scintilla Text Editor) is a lightweight and versatile text editor that can be used to create Java files on Ubuntu and other platforms. It supports syntax highlighting for various programming languages, including Java, making it suitable for code editing.


So, the correct answer is (C) SciTE.



12) Which of the following is right when Java file is created?

(A) Java source file & class name must be same.

(B) Java source file & class name must be different.

(C) Name must start with number only.

(D) Name must have symbol '$' at the end.

Answer: (A) Java source file & class name must be same.

Explanation:

In Java, the name of the Java source file must be the same as the name of the public class declared in that file. This is a requirement for the Java compiler to correctly compile the code. The public class is the entry point for the Java Virtual Machine (JVM) when executing the program.


So, the correct answer is (A) Java source file & class name must be the same.



13)  Which of the following is comment statement in Java?

(A) \* ... *\

(B) \# ... #\

(C) /* ... */

(D) /* ... /*

Answer: (C) /* ... */

Explanation:

In Java, the comment syntax for multi-line comments is `/* ... */`. Anything between `/*` and `*/` is treated as a comment and is ignored by the Java compiler. This allows developers to include comments and explanations within their code for documentation purposes.

Example:

/*

  This is a multi-line comment.

  It can span multiple lines.

  It is used for providing explanations and documentation.

*/


// This is a single-line comment.

// It is used for comments that span only one line.


So, the correct answer is (C) /* ... */.



14) Which of the following command is given in JDK using Java Interpreter?

(A) javarc CallCost

(B) run CallCost

(C) java CallCost

(D) javarun CallCost

Answer: (C) java CallCost

Explanation:

To run a Java program using the Java interpreter, you typically use the `java` command followed by the name of the class containing the `main` method. For example, if your class is named `CallCost`, you would run it using the following command:


java CallCost


This assumes that the class has a `main` method with the signature:


public static void main(String[] args) {

    // Your main program logic here

}


So, the correct answer is (C) `java CallCost`.



15) Which of the following is missing from compilation process to execute Java application?



(A) Java Bytecode file (*.class)

(B) Java Bytecode file (*.csc)

(C) Java Bookcode file (*.cla)

(D) Java Compilercode (*.javacr)

Answer: (A) Java Bytecode file (*.class)



16) How many basic (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, which are:


1. byte: 8-bit signed integer

2. short: 16-bit signed integer

3. int: 32-bit signed integer

4. long: 64-bit signed integer

5. float: 32-bit floating-point

6. double: 64-bit floating-point

7. char: 16-bit Unicode character

8. boolean: true or false


So, the correct answer is (C) 8.



17) Which of the following escape code is used for backspace in Java?

(A) \n

(B) \t

(C) \b

(D) \k

Answer: (C) \b

Explanation:

In Java, the escape code `\b` is used to represent the backspace character. When used in a string, it represents a backspace control character.


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



18) Which of the following loop is exit controlled loop in Java?

(A) for

(B) do..while

(C) for...while

(D) exit...while

Answer: (B) do..while

Explanation:

In Java, the `do..while` loop is an exit-controlled loop. This means that the loop body is executed at least once, and then the loop condition is checked. If the condition is true, the loop continues to execute; otherwise, it exits.

Example of a `do..while` loop:


int i = 1;

do {

    System.out.println("This is iteration " + i);

    i++;

} while (i <= 5);


In this example, the loop will always execute at least once, regardless of the condition.


So, the correct answer is (B) do..while.



19) Which of the following is not a part of four p's protection in Java?

(A) Public

(B) Package

(C) Private

(D) Provided

Answer: (D) Provided

Explanation:

In Java, the four access control levels are often referred to as the four "P"s of protection:


1. Public (`public`): Accessible from any other class.

2. Protected (`protected`): Accessible within the same package and by subclasses (even if they are in different packages).

3. Package-Private (default, no modifier): Accessible only within the same package.

4. Private (`private`): Accessible only within the same class.


There is no access control level called "Provided." Therefore, (D) Provided is not a part of the four P's of protection in Java.


 

20) Which of the following is the highest level of protection in Java?

(A) Public

(B) Package

(C) Private

(D) Provided

Answer: (C) Private

Explanation:

In Java, the visibility modifiers determine the level of access or protection for classes, methods, and variables. The highest level of protection is achieved with the `private` modifier.


- **Public (`public`):** Accessible from anywhere. There are no restrictions on its access.

- **Package (default):** Accessible within the same package. No modifier is used.

- **Private (`private`):** Accessible only within the same class.


So, the correct answer is (C) Private. 



21) Which of the following is the meaning of Polymorphism?

(A) Post morphism

(B) Post text

(D) Many forms

(C) Single form

Answer: (D) Many forms

Explanation:

Polymorphism in object-oriented programming refers to the ability of a single entity (such as a method or a class) to take many forms. There are two main types of polymorphism in Java:


1. Compile-time (Static) Polymorphism: This is achieved through method overloading and operator overloading. The decision on which method to call is made at compile time.


2. Runtime (Dynamic) Polymorphism: This is achieved through method overriding. The decision on which method to call is made at runtime based on the actual type of the object.


So, the correct answer is (D) Many forms.



22) Which of the following is known as a user defined no-argument constructor?

(A) <classname> ( ) [];

(B) <classname> ( ) {};

(C) <classname> {} [];

(D) <classname> [] ();

Answer: (B) <classname> ( ) {};

Explanation:

A user-defined no-argument constructor in Java has the following syntax:


public class ClassName {

    // User-defined no-argument constructor

    public ClassName() {

        // Constructor logic, if any

    }


    // Other members of the class

}


So, the correct answer is (B) <classname> ( ) {};



23) If we want Private data to be used by other, then which method is used?

(A) excess

(B) access

(C) accessor

(D) mutator

Answer: (C) accessor

Explanation:

If we want private data to be used by other classes or methods, we typically provide access to that data through accessor methods. Accessor methods are also known as "getters." These methods allow other classes to retrieve the values of private fields.

Example:

public class MyClass {

    private int privateData;


    // Accessor method (getter) for privateData

    public int getPrivateData() {

        return privateData;

    }

}


In this example, `getPrivateData()` is an accessor method that allows other classes to retrieve the value of the private field `privateData`.


So, the correct answer is (C) accessor.


 

24) Which of the following prefix is utilize for conventionally naming of Mutator method is used?

(A) get

(B) set

(C) wait

(D) take

Answer: (B) set

Explanation:

For conventionally naming mutator methods (methods that modify the state of an object), the prefix commonly used is "set." Mutator methods are also known as "setters." These methods are responsible for setting or updating the values of the object's attributes (fields).

Example:


public class MyClass {

    private int data;


    // Mutator method (setter) for data

    public void setData(int newData) {

        data = newData;

    }

}


In this example, `setData(int newData)` is a mutator method that sets the value of the private field `data`.


So, the correct answer is (B) set.


 

25) Instance variable and Instance methods are accessed via objects. They can be referred which of following operator?

(A) (.) dot

(B) (,) comma

(C) (“) quotes

(D) (;) semicolon

Answer: (A) (.) dot

Explanation:

Instance variables and instance methods in Java are accessed using the dot (`.`) operator. The dot operator is used to access members (fields or methods) of an object.

Example:


public class MyClass {

    // Instance variable

    private int myVariable;


    // Instance method

    public void myMethod() {

        // Method logic

    }

}


public class Main {

    public static void main(String[] args) {

        // Creating an object of MyClass

        MyClass obj = new MyClass();


        // Accessing instance variable using dot operator

        obj.myVariable = 10;


        // Accessing instance method using dot operator

        obj.myMethod();

    }

}


In this example, `obj.myVariable` and `obj.myMethod()` use the dot operator to access the instance variable and instance method of the object `obj`.


So, the correct answer is (A) (.) dot.



26) Which of the following variable defined within a class but outside any method?

(A) Local variable

(B) Instance variable

(C) Class variable

(D) Room variable

Answer: (B) Instance variable

Explanation:

A variable defined within a class but outside any method is called an instance variable. Instance variables are associated with instances (objects) of the class, and each instance of the class has its own copy of these variables.

Example:

public class MyClass {

    // Instance variable

    private int instanceVariable;


    // Other members of the class

}


In this example, `instanceVariable` is an instance variable defined within the class `MyClass`.


So, the correct answer is (B) Instance variable.


 

27) Which of the following variable defined inside methods or blocks?

(A) Local variable

(B) Instance variable

(C) Class variable

(D) Room variable

Answer: (A) Local variable

Explanation:

A variable defined inside methods or blocks is called a local variable. Local variables are limited to the scope of the method or block in which they are declared. They are not accessible outside that specific scope.

Example:

public class MyClass {

    // Instance variable

    private int instanceVariable;


    // Method with a local variable

    public void myMethod() {

        // Local variable

        int localVar = 10;


        // Code using localVar

    }

}


In this example, `localVar` is a local variable defined inside the method `myMethod`.


So, the correct answer is (A) Local variable.



28) Which of the following is a special kind method that is invoked when a new Object is created?

(A) Constructor

(B) Consider

(C) Conformation

(D) Console

Answer: (A) Constructor

Explanation:

A constructor is a special kind of method in Java that is invoked when an object is created. It has the same name as the class and is used to initialize the object's state. Constructors play a crucial role in the process of object creation.

Example:

public class MyClass {

    // Constructor

    public MyClass() {

        // Initialization logic

    }


    // Other members of the class

}


In this example, `MyClass()` is a constructor that is invoked when an object of the class `MyClass` is created.


So, the correct answer is (A) Constructor.



29) Which of the following are useful to represent vector, matrix and other multi-dimensional data?

(A) Element

(B) Variable

(C) Array

(D) Tag

Answer: (C) Array

Explanation:

Arrays in Java are useful for representing vector, matrix, and other multi-dimensional data structures. Arrays provide a way to store elements of the same data type in contiguous memory locations, making it convenient for accessing and manipulating data.

Example of a 2D array (matrix):


public class MatrixExample {

    public static void main(String[] args) {

        // Declaration and initialization of a 2D array

        int[][] matrix = {

            {1, 2, 3},

            {4, 5, 6},

            {7, 8, 9}

        };


        // Accessing elements of the matrix

        System.out.println(matrix[1][2]); // Output: 6

    }

}


In this example, `matrix` is a 2D array representing a matrix.


So, the correct answer is (C) Array.



30) Which of the following refer to the starting index value in arrays?

(A) 0

(B) 1

(C) null

(D) All of these

Answer: (A) 0

Explanation:

In most programming languages, including Java, the starting index value for arrays is typically 0. This means that the first element of the array is accessed using index 0.

Example:

public class ArrayExample {

    public static void main(String[] args) {

        // Declaration and initialization of an array

        int[] numbers = {10, 20, 30, 40, 50};


        // Accessing elements of the array

        System.out.println(numbers[0]); // Output: 10

        System.out.println(numbers[1]); // Output: 20

    }

}


In this example, `numbers[0]` refers to the first element of the array.


So, the correct answer is (A) 0.



31) If int marks [] = {1,2,3,4,5}; is defined in Java then what will be the value of marks [3]?

(A) 2

(B) 3

(C) 4

(D) 5

Answer: (C) 4

Explanation:

In Java, array indices start from 0. So, if `int marks[] = {1, 2, 3, 4, 5};` is defined, then the value of `marks[3]` will be the element at index 3, which is 4.


int marks[] = {1, 2, 3, 4, 5};

System.out.println(marks[3]); // Output: 4


Therefore, the correct answer is (C) 4.



32) Which of the following constructor is used for without arguments create a string object with no character?

(A) String ( )

(B) String [ ]

(C) String { }

(D) String < >

Answer: (A) String ( )

Explanation:

The constructor `String()` is used to create an empty string object with no characters. It creates a new string instance with a length of 0.

Example:

String emptyString = new String();

System.out.println(emptyString); // Output: ""


In this example, `emptyString` is a new string object created using the `String()` constructor, resulting in an empty string.


So, the correct answer is (A) String ( ).



33) Which of the following string class method returns true if invoking string is same as (str)?

(A) Boolean eq(String str)

(B) boolean equals(String str)

(C) boolean Equal(String str)

(D) Boolean Eq(String str)

Answer: (B) boolean equals(String str)

Explanation:

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

Example:

String str1 = "Hello";

String str2 = "Hello";

String str3 = "World";


System.out.println(str1.equals(str2)); // Output: true

System.out.println(str1.equals(str3)); // Output: false


In this example, `str1.equals(str2)` returns `true` because the content of both strings is the same.


So, the correct answer is (B) boolean equals(String str).


 

34) Which of the following string class method returns number of characters in invoking string?

(A) Int Length()

(B) int LENGTH()

(C) int length()

(D) Float int length()

Answer: (C) int length()

Explanation:

The `length()` method in the `String` class is used to get the number of characters in the invoking string. It returns an integer representing the length of the string.

Example:

String myString = "Hello, World!";

int length = myString.length();

System.out.println(length); // Output: 13


In this example, `myString.length()` returns `13` because the length of the string "Hello, World!" is 13 characters.


So, the correct answer is (C) int length().


 

35) Which Of the following Date Class method constructs Date object using current system time?

(A) Date [date ()]

(B) Date (cur ())

(C) Date (long ())

(D) Date ()

Answer: (D) Date()

Explanation:

The `Date()` constructor in the `Date` class is used to construct a `Date` object representing the current date and time based on the system clock.

Example:

import java.util.Date;


public class DateExample {

    public static void main(String[] args) {

        // Creating a Date object using the current system time

        Date currentDate = new Date();


        // Printing the current date and time

        System.out.println(currentDate);

    }

}


In this example, `new Date()` creates a `Date` object representing the current date and time.


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


36) Date class and Calendar class is provided in which of the following package?

(A) java.dc

(B) java.util

(C) java.class

(D) java.dateclass

Answer: (B) java.util

Explanation:

The `Date` class and `Calendar` class in Java are provided in the `java.util` package.


So, the correct answer is (B) java.util.


 

37) Which of the following constant is defined in calendar class for year of calendar?

(A) YRS

(B) YYYY

(C) YY

(D) YEAR

Answer: (D) YEAR

Explanation:

In the `Calendar` class of Java, the constant representing the field for the year of the calendar is `YEAR`.


So, the correct answer is (D) YEAR.


 

38) What is the size of second dimension in an array gseb[5][3]?

(A) 5

(B) 3

(C) 15

(D) 18

Answer: (B) 3

Explanation:

In the array declaration `gseb[5][3]`, the second dimension size is 3. This means that each element in the first dimension (`gseb[5]`) is an array of size 3.


So, the correct answer is (B) 3.


 

39) Errors can be broadly classified into how many categories?

(A) 6

(B) 2

(C) 5

(D) 3

Answer: (B) 2

Explanation:

Errors in programming can be broadly classified into two categories:


1. Compile-time Errors: These errors occur during the compilation of the program. They are also known as syntax errors and prevent the program from being compiled successfully.


2. Runtime Errors: These errors occur during the execution of the program. They are also known as exceptions and cause the program to terminate abnormally if not handled.


So, the correct answer is (B) 2.


 

40) Which of the following is not created when we get compilation error?

(A) .sci

(B) .html

(C) .class

(D) .java

Answer: (C) .class

Explanation:

- .class: This file contains the compiled bytecode and is generated when the compilation is successful.

- .java: This is the source file written in the Java programming language.


So, the correct answer is (C) .class


 

41) Which of the following exception class is invoke when there was an attempt to divide any number by zero?

(A) ArithmeticDivide

(B) Aithmeticlndex

(C) ExceptionArithmetic

(D) ArithmeticException

Answer: (D) ArithmeticException

Explanation:

In Java, when there is an attempt to divide any number by zero, it results in an `ArithmeticException` being thrown. This exception is part of the Java standard library and is specifically designed to handle arithmetic-related errors.


So, the correct answer is (D) ArithmeticException.



42) Which of the following block may give rise to one or more exception?

(A) catch

(B) finally

(C) try

(D) allow

Answer: (C) try

Explanation:

In Java exception handling, the `try` block contains the code that may give rise to one or more exceptions. The `try` block is followed by `catch` and/or `finally` blocks to handle or clean up after exceptions.


So, the correct answer is (C) try.



43) Which of the following block must immediately follow try block?

(A) catch

(B) final

(C) try

(D) excep

Answer: (A) catch

Explanation:

In Java exception handling, the `catch` block must immediately follow the `try` block. The `catch` block is used to catch and handle the exceptions thrown within the associated `try` block.


So, the correct answer is (A) catch. 



44) Which of the following block is generally used to clean up at the end of executing a try block?

(A) catch

(B) final

(C) finally

(D) excep

Answer: (C) finally

Explanation:

The `finally` block in Java is generally used to clean up at the end of executing a `try` block. The code inside the `finally` block will be executed whether an exception is thrown or not, making it suitable for cleanup operations such as closing files or releasing resources.


So, the correct answer is (C) finally. 



45) Which of the following is a temporary storage?

(A) Volatile storage

(B) Non-volatile storage

(C) Volate storage

(D) Valid storage

Answer: (A) Volatile storage

Explanation:

Volatile storage refers to temporary storage that loses its content when the power is turned off. RAM (Random Access Memory) is an example of volatile storage. Non-volatile storage, on the other hand, retains its content even when the power is turned off, and examples include hard drives, SSDs, and flash memory.


So, the correct answer is (A) Volatile storage.


 

46) Which of the following class encapsulates information about the properties of a file or a directory?

(A) java.io.FileDir

(B) java.io.FD

(C) java.io.Fi1e

(D) java.io.Director

Answer: (C) java.io.File

Explanation:

The `java.io.File` class in Java is used to encapsulate information about the properties of a file or a directory. It provides methods for performing operations on files and directories, such as creating, deleting, checking existence, and retrieving information about file properties.


So, the correct answer is (C) java.io.File.



47) Which Of the following file Class method returns true if the file exists, otherwise return false?

(A) boolean exists()

(B) boolean isFile()

(C) boolean getFile()

(D) boolean srcFile()

Answer: (A) boolean exists()

Explanation:

The `exists()` method in the `File` class is used to check whether the file or directory represented by the `File` object exists. It returns `true` if the file or directory exists, and `false` otherwise.

Example:

import java.io.File;


public class FileExample {

    public static void main(String[] args) {

        // Creating a File object

        File file = new File("example.txt");


        // Checking if the file exists

        if (file.exists()) {

            System.out.println("File exists");

        } else {

            System.out.println("File does not exist");

        }

    }

}


In this example, `file.exists()` is used to check whether the file "example.txt" exists.


So, the correct answer is (A) boolean exists().


 

48) Scanner class belongs to which of the following package?

(A) java.scan

(B) java.pack

(C) java.ioFile

(D) java.util

Answer: (D) java.util

Explanation:

The `Scanner` class in Java belongs to the `java.util` package. The `Scanner` class is used for parsing and processing user input and is commonly used to read input from the keyboard.


So, the correct answer is (D) java.util.


 

49) Which of the following scanner class method scans the next token of the input as line?

(A) String close()

(B) String next()

(C) String nextLine()

(D) Void close()

Answer: (C) String nextLine()

Explanation:

The `nextLine()` method in the `Scanner` class is used to scan the next token of the input as a line. It reads the input until a newline character is encountered, and then it returns the entire line as a string.


So, the correct answer is (C) String nextLine().


 

50) Which of the following class provides a method for reading password?

(A) Write class

(B) File class

(C) Console class

(D) Password class

Answer: (C) Console class

Explanation:

The `Console` class in Java provides a method for reading passwords without echoing characters to the console. The method for reading passwords is `readPassword()`.

Example:

import java.io.Console;


public class ConsoleExample {

    public static void main(String[] args) {

        Console console = System.console();


        if (console != null) {

            char[] password = console.readPassword("Enter password: ");

            System.out.println("Password entered: " + new String(password));

        } else {

            System.out.println("Console is not available");

        }

    }

}


In this example, `console.readPassword()` is used to read a password without echoing characters to the console.


So, the correct answer is (C) Console class.



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