250+ TOP MCQs on Random Number and Answers

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

1. Which class is used to generate random number?
a) java.lang.Object
b) java.util.randomNumber
c) java.util.Random
d) java.util.Object

Answer: c
Clarification: java.util.random class is used to generate random numbers in java program.

2. Which method is used to generate boolean random values in java?
a) nextBoolean()
b) randomBoolean()
c) previousBoolean()
d) generateBoolean()

Answer: a
Clarification: nextBoolean() method of java.util.Random class is used to generate random numbers.

3. What is the return type of Math.random() method?
a) Integer
b) Double
c) String
d) Boolean

Answer: b
Clarification: Math.random() method returns floating point number or precisely a double.

4. Random is a final class?
a) True
b) False

Answer: b
Clarification: Random is not a final class and can be extended to implement the algorithm as per requirement.

5. What is the range of numbers returned by Math.random() method?
a) -1.0 to 1.0
b) -1 to 1
c) 0 to 100
d) 0.0 to 1.0

Answer: d
Clarification: Math.random() returns only double value greater than or equal to 0.0 and less than 1.0.

6. How many bits are used for generating random numbers?
a) 32
b) 64
c) 48
d) 8

Answer: c
Clarification: Random number can accept 64 bits but it only uses 48 bits for generating random numbers.

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

int a = random.nextInt(15) + 1;

a) Random number between 1 to 15, including 1 and 15
b) Random number between 1 to 15, excluding 15
c) Random number between 1 to 15, excluding 1
d) Random number between 1 to 15, excluding 1 and 15

Answer: a
Clarification: random.nextInt(15) + 1; returns random numbers between 1 to 15 including 1 and 15.

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

int a = random.nextInt(7) + 4;

a) Random number between 4 to 7, including 4 and 7
b) Random number between 4 to 7, excluding 4 and 7
c) Random number between 4 to 10, excluding 4 and 10
d) Random number between 4 to 10, including 4 and 10

Answer: d
Clarification: random.nextInd(7) + 4; returns random numbers between 4 to 10 including 4 and 10. it follows “nextInt(max – min +1) + min” formula.

9. Math.random() guarantees uniqueness?
a) True
b) False

Answer: b
Clarification: Math.random() doesn’t guarantee uniqueness. To guarantee uniqueness we must store the generated value in the database and compare against already generated values.

10. What is the signature of Math.random() method?
a) public static double random()
b) public void double random()
c) public static int random()
d) public void int random()

Answer: a
Clarification: public static double random() is the utility method provided by Math class which returns double.

contest

250+ TOP MCQs on Wildcards and Answers

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

1. Which of these is wildcard symbol?
a) ?
b) !
c) %
d) &

Answer: a
Clarification: In generic code, the question mark (?), called the wildcard, represents an unknown type.

2. What is use of wildcards?
a) It is used in cases when type being operated upon is not known
b) It is used to make code more readable
c) It is used to access members of super class
d) It is used for type argument of generic method

Answer: a
Clarification: The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.

3. Which of these keywords is used to upper bound a wildcard?
a) stop
b) bound
c) extends
d) implements

Answer: c
Clarification: None.

4. Which of these is an correct way making a list that is upper bounded by class Number?
a) List
b) List
c) List(? extends Number)
d) List(? UpperBounds Number)

Answer: a
Clarification: None.

5. Which of the following keywords are used for lower bounding a wild card?
a) extends
b) super
c) class
d) lower

Answer: b
Clarification: A lower bounded wildcard is expressed using the wildcard character (‘?’), following by the super keyword, followed by its lower bound: super A>.

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

  1.     import java.util.*;
  2.     class Output
  3.     {
  4.         public static double sumOfList(List extends Number> list)
  5.         {
  6.             double s = 0.0;
  7.             for (Number n : list)
  8.                 s += n.doubleValue();
  9.             return s;
  10.         }
  11.         public static void main(String args[])
  12.         {
  13.             List<Integer> li = Arrays.asList(1, 2, 3);
  14.             System.out.println(sumOfList(li));
  15.         }
  16.     }

a) 0
b) 4
c) 5.0
d) 6.0

Answer: d
Clarification: None.
Output:

$ javac Output.javac
$ java Output
6.0

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

  1.     import java.util.*;
  2.     class Output 
  3.     {
  4.         public static double sumOfList(List extends Number> list)
  5.         {
  6.             double s = 0.0;
  7.             for (Number n : list)
  8.                 s += n.doubleValue();
  9.             return s;
  10.         }
  11.         public static void main(String args[]) 
  12.         {
  13.            List<Double> ld = Arrays.asList(1.2, 2.3, 3.5);
  14.            System.out.println(sumOfList(ld));
  15.         }
  16.     }

a) 5.0
b) 7.0
c) 8.0
d) 6.0

Answer: b
Clarification: None.
Output:

$ javac Output.javac
$ java Output
7.0

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

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. 	public void push(E obj)
  6.         {
  7.             stk.push(obj);
  8. 	}
  9. 	public E pop() 
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. 	}
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <Integer> gs = new genericstack<Integer>();
  20.             gs.push(36);
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) H
b) Hello
c) Runtime Error
d) Compilation Error

Answer: d
Clarification: generic stack object gs is defined to contain a string parameter but we are sending an integer parameter, which results in compilation error.
Output:

$ javac Output.javac
$ java Output

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.