Sunday, July 28, 2024

Type Casting In Java MCQ

 What is typecasting in Java?

a) Assigning a value to a variable

b) Converting a value from one data type to another

c) Defining the type of a variable explicitly

d) Declaring a variable with a specific data type


Which of the following is an implicit type conversion in Java?

a) Converting a smaller data type to a larger one

b) Converting a larger data type to a smaller one

c) Converting a string to an integer

d) Converting a character to a string


What is the result of casting a floating-point number to an integer in Java?

a) Truncation of the decimal part

b) Rounding up to the nearest integer

c) Rounding down to the nearest integer

d) Conversion to a string


What is the purpose of explicit typecasting in Java?

a) To convert a larger data type to a smaller one

b) To convert a smaller data type to a larger one

c) To avoid compilation errors

d) To define custom data types


Which keyword is used for explicit typecasting in Java?

a) cast

b) convert

c) (type)

d) typecast


What is the result of casting a double value to an integer when the double value is beyond the range of integer data type?

a) It results in a compilation error.

b) It throws a runtime exception.

c) The value is rounded to the nearest integer.

d) It results in an overflow.


When converting a higher data type to a lower data type, what might occur in Java?

a) Truncation of data

b) Loss of precision

c) No change in data

d) Conversion to a string


What happens when you cast an object of one class to another unrelated class in Java?

a) It results in a runtime error.

b) It throws a compilation error.

c) It's allowed but can lead to unexpected behavior.

d) It automatically converts to the new class type.


Which of the following types of casting requires explicit type conversion in Java?

a) Casting between integers

b) Casting between floats and doubles

c) Casting between primitive data types

d) Casting between objects of unrelated classes


In Java, what does narrowing conversion refer to?

a) Converting a smaller data type to a larger one

b) Converting a larger data type to a smaller one

c) Converting objects to primitive data types

d) Converting strings to numerical values


Answers:


b) Converting a value from one data type to another

a) Converting a smaller data type to a larger one

a) Truncation of the decimal part

b) To convert a smaller data type to a larger one

c) (type)

d) It results in an overflow.

b) Loss of precision

c) It's allowed but can lead to unexpected behavior.

c) Casting between primitive data types

b) Converting a larger data type to a smaller one


MCQs on Type Casting in Java

What is type casting in Java?


a) Converting one primitive data type into another

b) Converting an object from one class type to another

c) Changing the value of a variable

d) Declaring multiple variables of different types


Answer: a) Converting one primitive data type into another

Explanation: Type casting refers to converting one primitive data type into another, such as from int to double.


Which of the following is an example of implicit type casting?


a) int i = 10; double d = i;

b) double d = 10.5; int i = (int) d;

c) char c = (char) 65;

d) float f = (float) 3.14;


Answer: a) int i = 10; double d = i;

Explanation: Implicit casting happens when converting from a smaller type to a larger type without explicit casting.


Which type of casting requires explicit casting?


a) Implicit casting

b) Automatic casting

c) Widening casting

d) Narrowing casting


Answer: d) Narrowing casting

Explanation: Narrowing casting (e.g., double to int) requires explicit casting because it involves converting a larger type to a smaller type.


What will be the result of the following code snippet?


java

Copy code

double d = 9.78;

int i = (int) d;

System.out.println(i);

a) 9

b) 9.78

c) 10

d) Error: incompatible types


Answer: a) 9

Explanation: Explicit casting from double to int truncates the decimal part, resulting in 9.


What does the following code snippet do?


java

Copy code

char c = 'A';

int i = c;

System.out.println(i);

a) Prints the ASCII value of A

b) Prints A

c) Prints 65

d) Causes a compilation error


Answer: c) Prints 65

Explanation: The char type is implicitly cast to int, which prints the ASCII value of A.


What is the output of this code snippet?


java

Copy code

int i = 10;

double d = i;

System.out.println(d);

a) 10.0

b) 10

c) Error: incompatible types

d) 10.00


Answer: a) 10.0

Explanation: Implicit casting from int to double adds a decimal point to the value.


Which type of casting is used to convert a float to an int?


a) Implicit casting

b) Narrowing casting

c) Widening casting

d) Automatic casting


Answer: b) Narrowing casting

Explanation: Converting float to int is narrowing casting as it involves converting a larger type to a smaller type.


How can you convert a String to an int in Java?


a) int i = String.valueOf("123");

b) int i = (int) "123";

c) int i = Integer.parseInt("123");

d) int i = String.toInt("123");


Answer: c) int i = Integer.parseInt("123");

Explanation: Integer.parseInt() method converts a String to an int.


What will be the output of the following code?


java

Copy code

double d = 9.99;

int i = (int) d;

System.out.println(i);

a) 9

b) 9.99

c) 10

d) Error: incompatible types


Answer: a) 9

Explanation: Explicit casting from double to int truncates the decimal part.


What is the result of casting an int to a byte?


a) No change in value

b) Possible loss of data

c) Automatic widening

d) Error: incompatible types


Answer: b) Possible loss of data

Explanation: Casting int to byte can result in loss of data if the int value exceeds the range of a byte.


What is the output of the following code snippet?


java

Copy code

byte b = 100;

int i = b;

System.out.println(i);

a) 100

b) Error: incompatible types

c) 100.0

d) byte


Answer: a) 100

Explanation: Implicit casting from byte to int does not change the value, only the type.


Which method is used to convert a double to a String in Java?


a) String.valueOf(double)

b) String.toString(double)

c) String.convert(double)

d) String.parse(double)


Answer: a) String.valueOf(double)

Explanation: String.valueOf() method converts a double to its String representation.


What will be the output of the following code snippet?


java

Copy code

int i = 300;

byte b = (byte) i;

System.out.println(b);

a) 44

b) 300

c) Error: incompatible types

d) 300


Answer: a) 44

Explanation: int value 300 exceeds the range of byte, so it wraps around, resulting in 44.


How do you convert a float to a String?


a) String.valueOf(float)

b) Float.toString(float)

c) String.convert(float)

d) String.parseFloat(float)


Answer: a) String.valueOf(float)

Explanation: String.valueOf() method can be used to convert a float to its String representation.


What will be the result of the following code snippet?


java

Copy code

char c = 'Z';

int i = c + 1;

System.out.println((char) i);

a) [

b) Y

c) Z

d) Error: incompatible types


Answer: a) [

Explanation: The character 'Z' has an ASCII value of 90. Adding 1 results in 91, which corresponds to the character '['.


No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...