250+ TOP MCQs on Bitwise Operators and Answers

Java MCQs on Bitwise operators of Java Programming Language.

1. Which of these is not a bitwise operator?
a) &
b) &=
c) |=
d) <=

Answer: d
Clarification: <= is a relational operator.

2. Which operator is used to invert all the digits in a binary representation of a number?
a) ~
b) <<<
c) >>>
d) ^

Answer: a
Clarification: Unary not operator, ~, inverts all of the bits of its operand in binary representation.

3. On applying Left shift operator, <<, on integer bits are lost one they are shifted past which position bit?
a) 1
b) 32
c) 33
d) 31

Answer: d
Clarification: The left shift operator shifts all of the bits in a value to the left specified number of times. For each shift left, the high order bit is shifted out and lost, zero is brought in from the right. When a left shift is applied to an integer operand, bits are lost once they are shifted past the bit position 31.

4. Which right shift operator preserves the sign of the value?
a) <<
b) >>
c) <<=
d) >>=

Answer: b
Clarification: None.

5. Which of these statements are incorrect?
a) The left shift operator, <<, shifts all of the bits in a value to the left specified number of times
b) The right shift operator, >>, shifts all of the bits in a value to the right specified number of times
c) The left shift operator can be used as an alternative to multiplying by 2
d) The right shift operator automatically fills the higher order bits with 0

Answer: d
Clarification: The right shift operator automatically fills the higher order bit with its previous contents each time a shift occurs. This also preserves the sign of the value.

6. What will be the output of the following Java program?

  1.     class bitwise_operator 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int var1 = 42;
  6.             int var2 = ~var1;
  7.             System.out.print(var1 + " " + var2);     	
  8.         } 
  9.     }

a) 42 42
b) 43 43
c) 42 -43
d) 42 43

Answer: c
Clarification: Unary not operator, ~, inverts all of the bits of its operand. 42 in binary is 00101010 in using ~ operator on var1 and assigning it to var2 we get inverted value of 42 i:e 11010101 which is -43 in decimal.
output:

$ javac bitwise_operator.java
$ java bitwise_operator
42 -43

7. What will be the output of the following Java program?

  1.     class bitwise_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int a = 3;
  6.              int b = 6;
  7.  	     int c = a | b;
  8.              int d = a & b;             
  9.              System.out.println(c + " "  + d);
  10.         } 
  11.     }

a) 7 2
b) 7 7
c) 7 5
d) 5 2

Answer: a
Clarification: And operator produces 1 bit if both operand are 1. Or operator produces 1 bit if any bit of the two operands in 1.
output:

$ javac bitwise_operator.java
$ java bitwise_operator
7 2

8. What will be the output of the following Java program?

  1.     class leftshift_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {        
  5.              byte x = 64;
  6.              int i;
  7.              byte y; 
  8.              i = x << 2;
  9.              y = (byte) (x << 2)
  10.              System.out.print(i + " " + y);
  11.         } 
  12.     }

a) 0 64
b) 64 0
c) 0 256
d) 256 0

Answer: d
Clarification: None.
output:

$ javac leftshift_operator.java
$ java leftshift_operator
256 0

9. What will be the output of the following Java program?

  1.     class rightshift_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int x; 
  6.              x = 10;
  7.              x = x >> 1;
  8.              System.out.println(x);
  9.         } 
  10.     }

a) 10
b) 5
c) 2
d) 20

Answer: b
Clarification: Right shift operator, >>, devides the value by 2.
output:

$ javac rightshift_operator.java
$ java rightshift_operator
5

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int a = 1;
  6.              int b = 2;
  7.              int c = 3;
  8.              a |= 4;
  9.              b >>= 1;
  10.              c <<= 1;
  11.              a ^= c;
  12.              System.out.println(a + " " + b + " " + c);
  13.         } 
  14.     }

a) 3 1 6
b) 2 2 3
c) 2 3 4
d) 3 3 6

Answer: a
Clarification: None.
output:

$ javac Output.java
$ java Output
3 1 6

250+ TOP MCQs on String Class and Answers

Java MCQs on String class of Java Programming Language.

1. String in Java is a?
a) class
b) object
c) variable
d) character array

Answer: a
Clarification: None.

2. Which of these method of String class is used to obtain character at specified index?
a) char()
b) Charat()
c) charat()
d) charAt()

Answer: d
Clarification: None.

3. Which of these keywords is used to refer to member of base class from a subclass?
a) upper
b) super
c) this
d) none of the mentioned

Answer: b
Clarification: Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.

4. Which of these method of String class can be used to test to strings for equality?
a) isequal()
b) isequals()
c) equal()
d) equals()

Answer: d
Clarification: None.

5. Which of the following statements are incorrect?
a) String is a class
b) Strings in java are mutable
c) Every string is an object of class String
d) Java defines a peer class of String, called StringBuffer, which allows string to be altered

Answer: b
Clarification: Strings in Java are immutable that is they can not be modified.

6. What will be the output of the following Java program?

  1.     class string_demo 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String obj = "I" + "like" + "Java";   
  6.             System.out.println(obj);     
  7.         }
  8.    }

a) I
b) like
c) Java
d) IlikeJava

Answer: d
Clarification: Java defines an operator +, it is used to concatenate strings.
output:

$ javac string_demo.java
$ java string_demo
IlikeJava

7. What will be the output of the following Java program?

  1.     class string_class 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String obj = "I LIKE JAVA";   
  6.             System.out.println(obj.charAt(3));
  7.         } 
  8.     }

a) I
b) L
c) K
d) E

Answer: a
Clarification: charAt() is a method of class String which gives the character specified by the index. obj.charAt(3) gives 4th character i:e I.
output:

$ javac string_class.java
$ java string_class
I

8. What will be the output of the following Java program?

  1.     class string_class 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String obj = "I LIKE JAVA";   
  6.             System.out.println(obj.length());
  7.         }
  8.     }

a) 9
b) 10
c) 11
d) 12

Answer: c
Clarification: None.
output:

$ javac string_class.java
$ java string_class 
11

9. What will be the output of the following Java program?

  1.     class string_class 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String obj = "hello";
  6.             String obj1 = "world";   
  7.             String obj2 = obj;
  8.             obj2 = " world";
  9.             System.out.println(obj + " " + obj2);
  10.         }
  11.     }

a) hello hello
b) world world
c) hello world
d) world hello

Answer: c
Clarification: None.
output:

$ javac string_class.java
$ java string_class
hello world

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

  1.     class string_class 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String obj = "hello";
  6.             String obj1 = "world";   
  7.             String obj2 = "hello";
  8.             System.out.println(obj.equals(obj1) + " " + obj.equals(obj2));
  9.         }
  10.     }

a) false false
b) true true
c) true false
d) false true

Answer: d
Clarification: equals() is method of class String, it is used to check equality of two String objects, if they are equal, true is retuned else false.
output:

$ javac string_class.java
$ java string_class
false true

250+ TOP MCQs on Java.lang – Integer, Long & Character Wrappers and Answers

Java MCQs on Integer, Long & Character wrappers of Java Programming Language.

1. Which of these is a wrapper for data type int?
a) Integer
b) Long
c) Byte
d) Double

Answer: a
Clarification: None.

2. Which of the following methods is a method of wrapper Integer for obtaining hash code for the invoking object?
a) int hash()
b) int hashcode()
c) int hashCode()
d) Integer hashcode()

Answer: c
Clarification: None.

3. Which of these is a super class of wrappers Long, Character & Integer?
a) Long
b) Digits
c) Float
d) Number

Answer: d
Clarification: Number is an abstract class containing subclasses Double, Float, Byte, Short, Integer and Long.

4. Which of these is a wrapper for simple data type char?
a) Float
b) Character
c) String
d) Integer

Answer: b
Clarification: None.

5. Which of the following is method of wrapper Integer for converting the value of an object into int?
a) bytevalue()
b) int intValue();
c) Bytevalue()
d) Byte Bytevalue()

Answer: b
Clarification: None.

6. Which of these methods is used to obtain value of invoking object as a long?
a) long value()
b) long longValue()
c) Long longvalue()
d) Long Longvalue()

Answer: b
Clarification: long longValue() is used to obtain value of invoking object as a long.

7. What will be the output of the following Java program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             char a[] = {'a', '5', 'A', ' '};   
  6.             System.out.print(Character.isDigit(a[0]) + " ");
  7.             System.out.print(Character.isWhitespace(a[3]) + " ");
  8.             System.out.print(Character.isUpperCase(a[2]));
  9.         }
  10.     }

a) true false true
b) false true true
c) true true false
d) false false false

Answer: b
Clarification: Character.isDigit(a[0]) checks for a[0], whether it is a digit or not, since a[0] i:e ‘a’ is a character false is returned. a[3] is a whitespace hence Character.isWhitespace(a[3]) returns a true. a[2] is an uppercase letter i:e ‘A’ hence Character.isUpperCase(a[2]) returns true.
Output:

$ javac Output.java
$ java Output
false true true

8. What will be the output of the following Java program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             Integer i = new Integer(257);  
  6.             byte x = i.byteValue();
  7.             System.out.print(x);
  8.         }
  9.     }

a) 0
b) 1
c) 256
d) 257

Answer: b
Clarification: i.byteValue() method returns the value of wrapper i as a byte value. i is 257, range of byte is 256 therefore i value exceeds byte range by 1 hence 1 is returned and stored in x.
Output:

$ javac Output.java
$ java Output
1

9. What will be the output of the following Java program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             Integer i = new Integer(257);  
  6.             float x = i.floatValue();
  7.             System.out.print(x);
  8.         }
  9.     }

a) 0
b) 1
c) 257
d) 257.0

Answer: d
Clarification: None.
Output:

$ javac Output.java
$ java Output
257.0

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             Long i = new Long(256);  
  6.             System.out.print(i.hashCode());
  7.         }
  8.     }

a) 256
b) 256.0
c) 256.00
d) 257.00

Answer: a
Clarification: None.
Output:

$ javac Output.java
$ java Output
256

250+ TOP MCQs on Environment Properties and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Environment Properties”.

1. Which object Java application uses to create a new process?
a) Process
b) Builder
c) ProcessBuilder
d) CreateBuilder

Answer: c
Clarification: Java application uses ProcessBuilder object to create a new process. By default, same set of environment variables passed which are set in application’s virtual machine process.

2. Which of the following is true about Java system properties?
a) Java system properties are accessible by any process
b) Java system properties are accessible by processes they are added to
c) Java system properties are retrieved by System.getenv()
d) Java system properties are set by System.setenv()

Answer: b
Clarification: Java system properties are only used and accessible by the processes they are added.

3. Java system properties can be set at runtime.
a) True
b) False

Answer: a
Clarification: Java system properties can be set at runtime using System.setProperty(name, value) or using System.getProperties().load() methods.

4. Which system property stores installation directory of JRE?
a) user.home
b) java.class.path
c) java.home
d) user.dir

Answer: c
Clarification: java.home is the installation directory of Java Runtime Environment.

5. What does System.getProperty(“variable”) return?
a) compilation error
b) value stored in variable
c) runtime error
d) null

Answer: d
Clarification: System.getProperty(“variable”) returns null value. Because, variable is not a property and if property does not exist, this method returns null value.

6. What is true about the setProperties method?
a) setProperties method changes the set of Java Properties which are persistent
b) Changing the system properties within an application will affect future invocations
c) setProperties method changes the set of Java Properties which are not persistent
d) setProperties writes the values directly into the file which stores all the properties

Answer: c
Clarification: The changes made by the setProperties method are not persistent. Hence, it does not affect future invocation.

7. How to use environment properties in the class?
a) @Environment
b) @Variable
c) @Property
d) @Autowired

Answer: d
Clarification:

             @Autowired
	     private Environment env;

This is how environment variables are injected in the class where they can be used.

8. How to assign values to variable using property?
a)

@Value("${my.property}")
private String prop;

b)

@Property("${my.property}")
private String prop; 

c)

@Environment("${my.property}")
private String prop;

d)

@Env("${my.property}")
private String prop;

Answer: a
Clarification: @Value are used to inject the properties and assign them to variables.

 
 

9. Which environment variable is used to set java path?
a) JAVA
b) JAVA_HOME
c) CLASSPATH
d) MAVEN_HOME

Answer: b
Clarification: JAVA_HOME is used to store a path to the java installation.

10. How to read a classpath file?
a) InputStream in = this.getClass().getResource(“SomeTextFile.txt”);
b) InputStream in = this.getClass().getResourceClasspath(“SomeTextFile.txt”);
c) InputStream in = this.getClass().getResourceAsStream(“SomeTextFile.txt”);
d) InputStream in = this.getClass().getResource(“classpath:/SomeTextFile.txt”);

Answer: c
Clarification: This method can be used to load files using relative path to the package of the class.

250+ TOP MCQs on Java.util – BitSet & Date class and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Java.util – BitSet & Date class”.

1. Which of these class object has an architecture similar to that of array?
a) Bitset
b) Map
c) Hashtable
d) All of the mentioned

Answer: a
Clarification: Bitset class creates a special type of array that holds bit values. This array can increase in size as needed.

2. Which of these method is used to make a bit zero specified by the index?
a) put()
b) set()
c) remove()
d) clear()

Answer: d
Clarification: None.

3. Which of these method is used to calculate number of bits required to hold the BitSet object?
a) size()
b) length()
c) indexes()
d) numberofBits()

Answer: b
Clarification: None.

4. Which of these is a method of class Date which is used to search whether object contains a date before the specified date?
a) after()
b) contains()
c) before()
d) compareTo()

Answer: c
Clarification: before() returns true if the invoking Date object contains a date that is earlier than one specified by date, otherwise it returns false.

5. Which of these methods is used to retrieve elements in BitSet object at specific location?
a) get()
b) Elementat()
c) ElementAt()
d) getProperty()

Answer: a
Clarification: None.

6. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class Bitset
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             BitSet obj = new BitSet(5);
  7.             for (int i = 0; i < 5; ++i)
  8.                 obj.set(i);
  9.             obj.clear(2);
  10.             System.out.print(obj);
  11.         }
  12.     }

a) {0, 1, 3, 4}
b) {0, 1, 2, 4}
c) {0, 1, 2, 3, 4}
d) {0, 0, 0, 3, 4}

Answer: a
Clarification: None.
Output:

$ javac Bitset.java
$ java Bitset
{0, 1, 3, 4}

7. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class Bitset 
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             BitSet obj = new BitSet(5);
  7.             for (int i = 0; i < 5; ++i)
  8.                 obj.set(i);
  9.             obj.clear(2);
  10.             System.out.print(obj.length() + " " + obj.size());
  11.         }
  12.     }

a) 4 64
b) 5 64
c) 5 128
d) 4 128

Answer: b
Clarification: obj.length() returns the length allotted to object obj at time of initialization and obj.size() returns the size of current object obj, each BitSet element is given 16 bits therefore the size is 4 * 16 = 64, whereas length is still 5.
Output:

$ javac Bitset.java
$ java Bitset
5 64

8. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class Bitset
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             BitSet obj = new BitSet(5);
  7.             for (int i = 0; i < 5; ++i)
  8.                 obj.set(i);
  9.             System.out.print(obj.get(3));
  10.         }
  11.     }

a) 2
b) 3
c) 4
d) 5

Answer: a
Clarification: None.
Output:

$ javac Bitset.java
$ java Bitset
2

9. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class date
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             Date obj = new Date();
  7.             System.out.print(obj);
  8.         }
  9.     }

a) Prints Present Date
b) Runtime Error
c) Any Garbage Value
d) Prints Present Time & Date

Answer: d
Clarification: None.
Output:

$ javac date.java
$ java date
Tue Jun 11 11:29:57 PDT 2013

10. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class Bitset
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             BitSet obj1 = new BitSet(5);
  7.             BitSet obj2 = new BitSet(10);
  8.             for (int i = 0; i < 5; ++i)
  9.                 obj1.set(i);
  10.             for (int i = 3; i < 13; ++i)
  11.                 obj2.set(i);
  12.             obj1.and(obj2);
  13.             System.out.print(obj1);
  14.         }
  15.     }

a) {0, 1}
b) {2, 4}
c) {3, 4}
d) {3, 4, 5}

Answer: c
Clarification: obj1.and(obj2) returns an BitSet object which contains elements common to both the object obj1 and obj2 and stores this BitSet in invoking object that is obj1. Hence obj1 contains 3 & 4.
Output:

$ javac Bitset.java
$ java Bitset
{3, 4}

250+ TOP MCQs on Thread class and Answers

Java MCQs on Thread class of Java Programming Language.

1. Which of these method of Thread class is used to find out the priority given to a thread?
a) get()
b) ThreadPriority()
c) getPriority()
d) getThreadPriority()

Answer: c
Clarification: None.

2. Which of these method of Thread class is used to Suspend a thread for a period of time?
a) sleep()
b) terminate()
c) suspend()
d) stop()

Answer: a
Clarification: None.

3. Which function of pre defined class Thread is used to check weather current thread being checked is still running?
a) isAlive()
b) Join()
c) isRunning()
d) Alive()

Answer: a
Clarification:isAlive() function is defined in class Thread, it is used for implementing multithreading and to check whether the thread called upon is still running or not.

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

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             t.setName("New Thread");
  7.             System.out.println(t);        
  8.         }
  9.     }

a) Thread[5,main]
b) Thread[New Thread,5]
c) Thread[main,5,main]
d) Thread[New Thread,5,main]

Answer: d
Clarification: None.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,5,main]

5. What is the priority of the thread in output in the following Java program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             t.setName("New Thread");
  7.             System.out.println(t.getName());        
  8.         }
  9.     }

a) main
b) Thread
c) New Thread
d) Thread[New Thread,5,main]

Answer: c
Clarification: The getName() function is used to obtain the name of the thread, in this code the name given to thread is ‘New Thread’.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
New Thread

6. What is the name of the thread in output in the following Java program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t.getPriority());        
  7.         }
  8.     }

a) 0
b) 1
c) 4
d) 5

Answer: d
Clarification: The default priority given to a thread is 5.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
5

7. What is the name of the thread in output in the following Java program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t.isAlive());        
  7.         }
  8.     }

a) 0
b) 1
c) true
d) false

Answer: c
Clarification: Thread t is seeded to currently program, hence when you run the program the thread becomes active & code ‘t.isAlive’ returns true.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
true