Saturday, August 10, 2024

Converting an integer to a string MCQ

 Here are multiple-choice questions (MCQs) on converting an integer to a string in Java:


Question 1

Which of the following methods can be used to convert an integer to a string in Java?


A) Integer.toString()


B) String.valueOf()


C) Integer.toString(int)


D) All of the above


Answer: D) All of the above


Question 2

What will be the output of the following code?


java

Copy code

int number = 123;

String str = Integer.toString(number);

System.out.println(str);

A) 123


B) "123"


C) Error


D) null


Answer: B) "123"


Question 3

Which method is more efficient for converting an integer to a string, String.valueOf() or Integer.toString()?


A) String.valueOf() is more efficient.


B) Integer.toString() is more efficient.


C) Both are equally efficient.


D) new String(int) is more efficient.


Answer: C) Both are equally efficient.


Question 4

What will the following code return?


java

Copy code

String str = String.valueOf(456);

A) "456"


B) 456


C) null


D) Error


Answer: A) "456"


Question 5

What does the following code do?


java

Copy code

int num = 789;

String str = "" + num;

A) Converts the integer 789 to a string "789" by concatenation.


B) Converts the integer 789 to a string "789" using String.valueOf().


C) Results in a compilation error.


D) Converts the integer 789 to a string "789" using Integer.toString().


Answer: A) Converts the integer 789 to a string "789" by concatenation.


Question 6

What is the output of the following code?


java

Copy code

int number = 0;

String str = Integer.toString(number);

System.out.println(str);

A) "0"


B) 0


C) null


D) "null"


Answer: A) "0"


Question 7

Which of the following is NOT a valid way to convert an integer to a string in Java?


A) Integer.toString(int)


B) String.valueOf(int)


C) Integer.toString()


D) "" + int


Answer: C) Integer.toString()


Question 8

What is the result of the following code?


java

Copy code

String str = Integer.toString(1234, 2);

A) "1234"


B) "1234" in binary


C) "10011010010"


D) Error


Answer: C) "10011010010"


Question 9

Can the String.valueOf() method convert an integer array to a string?


A) Yes, it converts each element to a string.


B) No, it throws an exception.


C) Yes, it converts the array's memory address to a string.


D) No, it returns "null".


Answer: C) Yes, it converts the array's memory address to a string.


Question 10

What will be the output of the following code?


java

Copy code

int num = 2147483647;

String str = Integer.toString(num);

System.out.println(str);

A) "2147483647"


B) 2147483647


C) -2147483647


D) Error


Answer: A) "2147483647"


Question 11

Which of the following can be used to convert an integer to a string with a specific radix?


A) String.valueOf(int, int)


B) Integer.toString(int, int)


C) String.valueOf(int, radix)


D) Integer.parseInt(int, int)


Answer: B) Integer.toString(int, int)


Question 12

What does the following code do?


java

Copy code

String str = String.valueOf(0);

A) Converts the integer 0 to the string "0".


B) Converts the integer 0 to the string "null".


C) Throws a NullPointerException.


D) Does not compile.


Answer: A) Converts the integer 0 to the string "0".


Question 13

What will be the output of the following code?


java

Copy code

int num = -123;

String str = Integer.toString(num);

System.out.println(str);

A) "123"


B) "-123"


C) "0"


D) Error


Answer: B) "-123"


Question 14

What is the result of Integer.toString(10, 16)?


A) "10"


B) "A"


C) "16"


D) "10" in hexadecimal


Answer: B) "A"


Question 15

Can you use String.valueOf() to convert a primitive int to a string?


A) Yes


B) No


C) Only with autoboxing


D) Only if the int is not null


Answer: A) Yes


Question 16

Which method provides more control over the conversion of an int to a string?


A) Integer.parseInt()


B) Integer.toString()


C) String.valueOf()


D) Both B and C


Answer: B) Integer.toString()


Question 17

What is the return type of Integer.toString(int)?


A) int


B) String


C) Integer


D) void


Answer: B) String


Question 18

What will the following code output?


java

Copy code

String str = Integer.toString(-32768);

System.out.println(str);

A) "32768"


B) "-32768"


C) "0"


D) Error


Answer: B) "-32768"


Question 19

Which method can be used to convert an integer to a string representation in a different number system, like binary?


A) Integer.toBinaryString(int)


B) Integer.toString(int, 2)


C) String.valueOf(int, 2)


D) A and B


Answer: D) A and B


Question 20

What happens if Integer.toString() is called with an integer value that exceeds Integer.MAX_VALUE?


A) It throws an exception.


B) It returns "2147483647".


C) It returns the string representation of Integer.MAX_VALUE.


D) This scenario is impossible; integers can't exceed Integer.MAX_VALUE.


Answer: D) This scenario is impossible; integers can't exceed Integer.MAX_VALUE.

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