Saturday, August 10, 2024

String to an integer in Java MCQ

 converting a string to an integer in Java:

Question 1

Which method is commonly used to convert a String to an int in Java?


A) Integer.toString()


B) String.parseInt()


C) Integer.parseInt()


D) Integer.valueOfString()


Answer: C) Integer.parseInt()


Question 2

What will be the output of the following code?


java

Copy code

String str = "123";

int number = Integer.parseInt(str);

System.out.println(number);

A) 123


B) "123"


C) Error


D) 0


Answer: A) 123


Question 3

What exception is thrown if the string passed to Integer.parseInt() cannot be converted to an integer?


A) NullPointerException


B) NumberFormatException


C) IllegalArgumentException


D) ArithmeticException


Answer: B) NumberFormatException


Question 4

Which of the following code snippets correctly converts a string "456" to an integer?


A) int num = Integer.valueOf("456");


B) int num = Integer.parseInt("456");


C) int num = new Integer("456");


D) All of the above


Answer: D) All of the above


Question 5

What is the difference between Integer.parseInt() and Integer.valueOf() when converting a string to an integer?


A) Integer.parseInt() returns an Integer object, while Integer.valueOf() returns an int.


B) Integer.parseInt() returns an int, while Integer.valueOf() returns an Integer object.


C) Integer.parseInt() throws a NumberFormatException for null strings, while Integer.valueOf() does not.


D) There is no difference.


Answer: B) Integer.parseInt() returns an int, while Integer.valueOf() returns an Integer object.


Question 6

Which of the following is a valid syntax to parse a string "789" into an int using Integer.parseInt()?


A) Integer.parseInt(789);


B) Integer.parseInt("789");


C) Integer.parseInt(new String("789"));


D) B and C


Answer: D) B and C


Question 7

What will happen if the string "12.34" is passed to Integer.parseInt()?


A) It will return 12.


B) It will throw a NumberFormatException.


C) It will return 0.


D) It will return 1234.


Answer: B) It will throw a NumberFormatException.


Question 8

What will be the output of the following code?


java

Copy code

String str = null;

int number = Integer.parseInt(str);

System.out.println(number);

A) 0


B) null


C) It will throw a NullPointerException.


D) It will throw a NumberFormatException.


Answer: D) It will throw a NumberFormatException.


Question 9

Is it possible to convert a string containing only whitespace characters to an int using Integer.parseInt()?


A) Yes


B) No


C) Only if whitespace is trimmed first


D) Only for leading whitespace


Answer: B) No


Question 10

Which class in Java provides the parseInt method?


A) String


B) Integer


C) Double


D) Math


Answer: B) Integer


Question 11

What will the following code return?


java

Copy code

Integer.valueOf("010");

A) 10


B) 8


C) 2


D) It will throw a NumberFormatException.


Answer: A) 10


Question 12

Can Integer.parseInt() be used with a radix other than 10?


A) No, it always uses decimal.


B) Yes, by passing the radix as a second argument.


C) Yes, by changing the default radix.


D) No, it's only for hexadecimal.


Answer: B) Yes, by passing the radix as a second argument.


Question 13

What will the following code output?


java

Copy code

String str = "0";

int number = Integer.parseInt(str);

System.out.println(number);

A) 0


B) "0"


C) Null


D) It will throw a NumberFormatException.


Answer: A) 0


Question 14

Which method would you use to convert a string "1024" into an Integer object?


A) Integer.getInteger("1024");


B) Integer.toString("1024");


C) Integer.valueOf("1024");


D) Integer.parseInt("1024");


Answer: C) Integer.valueOf("1024");


Question 15

What will be the result of Integer.parseInt("1000", 2);?


A) 1000


B) 8


C) 16


D) It will throw a NumberFormatException.


Answer: D) It will throw a NumberFormatException.


Question 16

What will be the result of Integer.parseInt("FF", 16);?


A) 255


B) 256


C) 127


D) It will throw a NumberFormatException.


Answer: A) 255


Question 17

What will be the output of the following code?


java

Copy code

String str = "2147483648";

int number = Integer.parseInt(str);

System.out.println(number);

A) 2147483648


B) -2147483648


C) It will throw a NumberFormatException.


D) 0


Answer: C) It will throw a NumberFormatException.


Question 18

How can you convert a hexadecimal string "1A" to an integer?


A) Integer.parseInt("1A");


B) Integer.parseInt("1A", 16);


C) Integer.parseInt("1A", 10);


D) Integer.valueOf("1A", 10);


Answer: B) Integer.parseInt("1A", 16);


Question 19

Which of the following will convert the string "007" to the integer 7?


A) Integer.valueOf("007");


B) Integer.parseInt("007");


C) new Integer("007");


D) All of the above


Answer: D) All of the above


Question 20

If Integer.parseInt("abc123") is executed, what will be the result?


A) It will return 123.


B) It will return 0.


C) It will return -1.


D) It will throw a NumberFormatException.


Answer: D) It will throw a NumberFormatException.

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