Java Quizs Question & Answers – Character and Boolean Data Types

Q 1. What will be the output of the following Java program?

  1. class mainclass {
  2. public static void main(String args[])
  3. {
  4. char a = ‘A’;
  5. a++;
  6. System.out.print((int)a);
  7. }
  8. }

A. 64
B. 65
C. 67
D. 66

Show Answer Answer:-D. 66
Explanation ASCII value of ‘A’ is 65, on using ++ operator character value increments by one. output:$ javac mainclass.java $ java mainclass 66

Q 2. What will be the output of the following Java program?

  1. class mainclass {
  2. public static void main(String args[])
  3. {
  4. boolean var1 = true;
  5. boolean var2 = false;
  6. if (var1)
  7. System.out.println(var1);
  8. else
  9. System.out.println(var2);
  10. }
  11. }

A. 0
B. 1
C. true
D. false

Show Answer Answer:-C. true
Explanation None. output:$ javac mainclass.java $ java mainclass true

Q 3. What will be the output of the following Java code?

  1. class booloperators {
  2. public static void main(String args[])
  3. {
  4. boolean var1 = true;
  5. boolean var2 = false;
  6. System.out.println((var1 & var2));
  7. }
  8. }

A. 0
B. 1
C. true
D. false

Show Answer Answer:-D. false
Explanation boolean ‘&’ operator always returns true or false. It returns true when both the values are true and false otherwise. Since, var1 is defined true and var2 is defined false hence their ‘&’ operator result is false. output:$ javac booloperators.java $ java booloperators false

Q 4. What will be the output of the following Java code?

  1. class asciicodes {
  2. public static void main(String args[]
  3. {
  4. char var1 = ‘A’;
  5. char var2 = ‘a’;
  6. System.out.println((int)var1 + ” ” + (int)var2);
  7. }
  8. }

A. 162
B. 67 95
C. 65 97
D. 66 98

Show Answer Answer:-C. 65 97
Explanation ASCII code for ‘A’ is 65 and for ‘a’ is 97. output:$ javac asciicodes.java $ java asciicodes 65 97

Q 5. What is the numerical range of a char data type in Java?
A. -128 to 127
B. 0 to 256
C. 0 to 32767
D. 0 to 65535

Show Answer Answer:-D. 0 to 65535
Explanation Char occupies 16-bit in memory, so it supports 216 i:e from 0 to 65535.

Q 6. Which of these coding types is used for data type characters in Java?
A. ASCII
B. ISO-LATIN-1
C. UNICODE
D. None of the mentioned

Show Answer Answer:-C. UNICODE
Explanation Unicode defines fully international character set that can represent all the characters found in all human languages. Its range is from 0 to 65536.

Q 7. Which of these values can a boolean variable contain?
A. True & False
B. 0 & 1
C. Any integer value
D. true

Show Answer Answer:-A. True & False
Explanation Boolean variable can contain only one of two possible values, true and false.

Q 8. Which of these occupy first 0 to 127 in Unicode character set used for characters in Java?
A. ASCII
B. ISO-LATIN-1
C. None of the mentioned
D. ASCII and ISO-LATIN1

Show Answer Answer:-D. ASCII and ISO-LATIN1
Explanation First 0 to 127 character set in Unicode are same as those of ISO-LATIN-1 and ASCII.

Q 9. Which one is a valid declaration of a boolean?
A. boolean b1 = 1;
B. boolean b2 = ‘false’;
C. boolean b3 = false;
D. boolean b4 = ‘true’

Show Answer Answer:-C. boolean b3 = false;
Explanation Boolean can only be assigned true or false literals.

Q 10. What will be the output of the following Java program?

  1. class array_output {
  2. public static void main(String args[])
  3. {
  4. char array_variable [] = new char[10];
  5. for (int i = 0; i < 10; ++i) {
  6. sarray_variable[i] = ‘i’;
  7. System.out.print(array_variable[i] + “” );
  8. i++;
  9. }
  10. }
  11. }

A. i i i i i
B. 0 1 2 3 4
C. i j k l m
D. None of the mentioned

Show Answer Answer:-A. i i i i i
Explanation output:$ javac array_output.java $ java array_output i i i i i

Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page