100+ Java Programming MCQs

                     

Java Questions & Answers – Integer and Floating Data Types

1. What is the range of short data type in Java?
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
d) None of the mentioned
Answer: b
2. What is the range of byte data type in Java?
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
d) None of the mentioned
Answer: a
3. Which of the following are legal lines of Java code?

a. int w = (int)888.8;
b. byte x = (byte)100L;
c. long y = (byte)100;
d. byte z = (byte)100L;

a) 1 and 2
b) 2 and 3
c) 3 and 4
d) All statements are correct
Answer: d
4. An expression involving byte, int, and literal numbers is promoted to which of these?
a) int
b) long
c) byte
d) float
Answer: a
5. Which of these literals can be contained in float data type variable?
a) -1.7e+308
b) -3.4e+038
c) +1.7e+308
d) -3.4e+050
Answer: b
6. Which data type value is returned by all transcendental math functions?
a) int
b) float
c) double
d) long
Answer: c
7. What will be the output of the following Java code?

      class average {
        public static void main(String args[])
        {
            double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
            double result;
            result = 0;
            for (int i = 0; i < 6; ++i) 
                result = result + num[i];
	    System.out.print(result/6);
 
        } 
    }

a) 16.34
b) 16.566666644
c) 16.46666666666667
d) 16.46666666666666
Answer: c
8. What will be the output of the following Java statement?

class output {
        public static void main(String args[]) 
        {
            double a, b,c;
            a = 3.0/0;
            b = 0/4.0;
            c=0/0.0;
 
	    System.out.println(a);
            System.out.println(b);
            System.out.println(c);
        } 
    }

a) Infinity
b) 0.0
c) NaN
d) all of the mentioned
Answer: d
9. What will be the output of the following Java code?

     class increment {
        public static void main(String args[]) 
        {        
             int g = 3;
             System.out.print(++g * 8);
        } 
    }

a) 25
b) 24
c) 32
d) 33
Answer: c
10. What will be the output of the following Java code?

      class area {
        public static void main(String args[]) 
        {    
             double r, pi, a;
             r = 9.8;
             pi = 3.14;
             a = pi * r * r;
             System.out.println(a);
        } 
    }

a) 301.5656
b) 301
c) 301.56
d) 301.56560000
Answer: a

Java Questions & Answers – Character and Boolean Data Types

1. 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
Answer: d
2. 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
Answer: c
3. Which of these values can a boolean variable contain?
a) True & False
b) 0 & 1
c) Any integer value
d) true
Answer: a
4. 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
Answer: d
5. 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’
Answer: c
6. What will be the output of the following Java program?

    class array_output {
        public static void main(String args[]) 
        {    
            char array_variable [] = new char[10];
	    for (int i = 0; i < 10; ++i) {
                array_variable[i] = 'i';
                System.out.print(array_variable[i] + "" );
                i++;
            }
        } 
    }

a) i i i i i
b) 0 1 2 3 4
c) i j k l m
d) None of the mentioned
Answer: a
7. What will be the output of the following Java program?

    class mainclass {
        public static void main(String args[]) 
        {
            char a = 'A';
            a++;
	    System.out.print((int)a);
        } 
    }

a) 66
b) 67
c) 65
d) 64
Answer: a
8. What will be the output of the following Java program?

    class mainclass {
        public static void main(String args[]) 
        {
            boolean var1 = true;
	    boolean var2 = false;
	    if (var1)
	        System.out.println(var1);
	    else
	        System.out.println(var2);
       } 
    }

a) 0
b) 1
c) true
d) false
Answer: c
9. What will be the output of the following Java code?

    class booloperators {
        public static void main(String args[]) 
        {
            boolean var1 = true;
	    boolean var2 = false;
	    System.out.println((var1 & var2));
        } 
    }

a) 0
b) 1
c) true
d) false
Answer: d
10. What will be the output of the following Java code?

    class asciicodes {
        public static void main(String args[]) 
        {
            char var1 = 'A';
	    char var2 = 'a';
	    System.out.println((int)var1 + " " + (int)var2);
        } 
    }

a) 162
b) 65 97
c) 67 95
d) 66 98
Answer: b

Java Questions & Answers – Data Type-Enums

1. What is the order of variables in Enum?
a) Ascending order
b) Descending order
c) Random order
d) Depends on the order() method
Answer: a
2. Can we create an instance of Enum outside of Enum itself?
a) True
b) False
Answer: b
3. What will be the output of the following Java code?

    enum Season 
    {
        WINTER, SPRING, SUMMER, FALL
    };
    System.out.println(Season.WINTER.ordinal());

a) 0
b) 1
c) 2
d) 3
Answer: a
4. If we try to add Enum constants to a TreeSet, what sorting order will it use?
a) Sorted in the order of declaration of Enums
b) Sorted in alphabetical order of Enums
c) Sorted based on order() method
d) Sorted in descending order of names of Enums
Answer: a
5. What will be the output of the following Java code snippet?

class A
{
 
}
 
enum Enums extends A
{
    ABC, BCD, CDE, DEF;
}

a) Runtime Error
b) Compilation Error
c) It runs successfully
d) EnumNotDefined Exception
Answer: b
6. What will be the output of the following Java code snippet?

 enum Levels 
{
    private TOP,
 
    public MEDIUM,
 
    protected BOTTOM;
}

a) Runtime Error
b) EnumNotDefined Exception
c) It runs successfully
d) Compilation Error
Answer: d
7. What will be the output of the following Java code snippet?

enum Enums
{
    A, B, C;
 
    private Enums()
    {
        System.out.println(10);
    }
}
 
public class MainClass
{
    public static void main(String[] args)
    {
        Enum en = Enums.B;
    }
}

a)

   10

   10

   10

b) Compilation Error
c)

   10

   10

d) Runtime Exception
Answer: a
8. Which method returns the elements of Enum class?
a) getEnums()
b) getEnumConstants()
c) getEnumList()
d) getEnum()
Answer: b
9. Which class does all the Enums extend?
a) Object
b) Enums
c) Enum
d) EnumClass
Answer: c
10. Are enums are type-safe?
a) True
b) False
Answer: a

Java Questions & Answers – Data Type-BigDecimal

1. Which of the following is the advantage of BigDecimal over double?
a) Syntax
b) Memory usage
c) Garbage creation
d) Precision
Answer: d
2. Which of the below data type doesn’t support overloaded methods for +,-,* and /?
a) int
b) float
c) double
d) BigDecimal
Answer: d
3. What will be the output of the following Java code snippet?

   double a = 0.02;
   double b = 0.03;
   double c = b - a;
   System.out.println(c);
 
   BigDecimal _a = new BigDecimal("0.02");
   BigDecimal _b = new BigDecimal("0.03");
   BigDecimal _c = b.subtract(_a);
   System.out.println(_c);

a)

   0.009999999999999998

   0.01

b)

   0.01

   0.009999999999999998

c)

   0.01

   0.01

d)

   0.009999999999999998

   0.009999999999999998

Answer: a
4. What is the base of BigDecimal data type?
a) Base 2
b) Base 8
c) Base 10
d) Base e
Answer: c
5. What is the limitation of toString() method of BigDecimal?
a) There is no limitation
b) toString returns null
c) toString returns the number in expanded form
d) toString uses scientific notation
Answer: d
6. Which of the following is not provided by BigDecimal?
a) scale manipulation
b) + operator
c) rounding
d) hashing
Answer: b
7. BigDecimal is a part of which package?
a) java.lang
b) java.math
c) java.util
d) java.io
Answer: b
8. What is BigDecimal.ONE?
a) wrong statement
b) custom defined statement
c) static variable with value 1 on scale 10
d) static variable with value 1 on scale 0
Answer: d
9. Which class is a library of functions to perform arithmetic operations of BigInteger and BigDecimal?
a) MathContext
b) MathLib
c) BigLib
d) BigContext
Answer: a
10. What will be the output of the following Java code snippet?

public class AddDemo 
{
	public static void main(String args[]) 
        {
		BigDecimal b = new BigDecimal("23.43");
		BigDecimal br = new BigDecimal("24");
		BigDecimal bres = b.add(new BigDecimal("450.23"));
		System.out.println("Add: "+bres);
 
		MathContext mc = new MathContext(2, RoundingMode.DOWN);
		BigDecimal bdecMath = b.add(new BigDecimal("450.23"), mc);
		System.out.println("Add using MathContext: "+bdecMath);
	}
}

a) Compilation failure
b)

Add: 473.66

Add using MathContext: 4.7E+2

c)

Add 4.7E+2

Add using MathContext: 473.66

d) Runtime exception
Answer: b

Java Questions & Answers – Data Type-Date, TimeZone

1. How to format date from one form to another?
a) SimpleDateFormat
b) DateFormat
c) SimpleFormat
d) DateConverter

Answer: a

2. How to convert Date object to String?
a)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.parse(new Date());

b)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.format(new Date());

c)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().parse();

d)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().format();

Answer: b

3. How to convert a String to a Date object?
a)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.parse(new Date());

b)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.format(new Date());

c)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().parse();

d)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().format();

Answer: a
4. Is SimpleDateFormat thread safe?
a) True
b) False
Answer: b
5. How to identify if a timezone is eligible for DayLight Saving?
a) useDaylightTime() of Time class
b) useDaylightTime() of Date class
c) useDaylightTime() of TimeZone class
d) useDaylightTime() of DateTime class
Answer: c
6. What is the replacement of joda time library in java 8?
a) java.time (JSR-310)
b) java.date (JSR-310)
c) java.joda
d) java.jodaTime
Answer: a
7. How is Date stored in database?
a) java.sql.Date
b) java.util.Date
c) java.sql.DateTime
d) java.util.DateTime
Answer: a
8. What does LocalTime represent?
a) Date without time
b) Time without Date
c) Date and Time
d) Date and Time with timezone
Answer: b
9. How to get difference between two dates?
a) long diffInMilli = java.time.Duration.between(dateTime1, dateTime2).toMillis();
b) long diffInMilli = java.time.difference(dateTime1, dateTime2).toMillis();
c) Date diffInMilli = java.time.Duration.between(dateTime1, dateTime2).toMillis();
d) Time diffInMilli = java.time.Duration.between(dateTime1, dateTime2).toMillis();
Answer: a
10. How to get UTC time?
a) Time.getUTC();
b) Date.getUTC();
c) Instant.now();
d) TimeZone.getUTC();
Answer: c

Java Questions & Answers – Literals & Variables

1. Which of these is long data type literal?
a) 0x99fffL
b) ABCDEFG
c) 0x99fffa
d) 99671246
Answer: a
2. Which of these can be returned by the operator &?
a) Integer
b) Boolean
c) Character
d) Integer or Boolean
Answer: d
3. Literals in java must be appended by which of these?
a) L
b) l
c) D
d) L and I
Answer: d
4. Literal can be of which of these data types?
a) integer
b) float
c) boolean
d) all of the mentioned
Answer: d
5. Which of these can not be used for a variable name in Java?
a) identifier
b) keyword
c) identifier & keyword
d) none of the mentioned
Answer: b
6. What will be the output of the following Java program?

    class evaluate 
    {
        public static void main(String args[]) 
        {
            int a[] = {1,2,3,4,5};
	    int d[] = a;
	    int sum = 0;
	    for (int j = 0; j < 3; ++j)
                sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);
	    System.out.println(sum);
        } 
    }

a) 38
b) 39
c) 40
d) 41
Answer: c
7. What will be the output of the following Java program?

    class array_output 
    {
        public static void main(String args[]) 
        {
       	    int array_variable [] = new int[10];
	    for (int i = 0; i < 10; ++i) {
                array_variable[i] = i/2;
                array_variable[i]++;
                System.out.print(array_variable[i] + " ");
                i++;
            }
 
        } 
    }

a) 0 2 4 6 8
b) 1 2 3 4 5
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
Answer: b
8. What will be the output of the following Java program?

    class variable_scope 
    {
        public static void main(String args[]) 
        {
            int x;
            x = 5;
            {
	        int y = 6;
	        System.out.print(x + " " + y);
            }
            System.out.println(x + " " + y);
        } 
    }

a) 5 6 5 6
b) 5 6 5
c) Runtime error
d) Compilation error
Answer: d
9. Which of these is an incorrect string literal?
a) “Hello World”
b) “Hello\nWorld”
c) “\”Hello World\””
d)”Hello

world”

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

    class dynamic_initialization 
    {
        public static void main(String args[]) 
        {
            double a, b;
            a = 3.0;
            b = 4.0;
	    double c = Math.sqrt(a * a + b * b);
	    System.out.println(c);
        } 
    }

a) 5.0
b) 25.0
c) 7.0
d) Compilation Error
Answer: a

Java Questions & Answers – Type Conversions, Promotions and Castings

1. Which of these is necessary condition for automatic type conversion in Java?
a) The destination type is smaller than source type
b) The destination type is larger than source type
c) The destination type can be larger or smaller than source type
d) None of the mentioned
Answer: b
2. What is the prototype of the default constructor of this Java class?

    public class prototype { }

a) prototype( )
b) prototype(void)
c) public prototype(void)
d) public prototype( )
Answer: d
3. What will be the error in the following Java code?

    byte b = 50;
    b = b * 50;

a) b cannot contain value 100, limited by its range
b) * operator has converted b * 50 into int, which can not be converted to byte without casting
c) b cannot contain value 50
d) No error in this code
Answer: b
4. If an expression contains double, int, float, long, then the whole expression will be promoted into which of these data types?
a) long
b) int
c) double
d) float
Answer: c
5. What is Truncation is Java?
a) Floating-point value assigned to an integer type
b) Integer value assigned to floating type
c) Floating-point value assigned to an Floating type
d) Integer value assigned to floating type
Answer: a
6. What will be the output of the following Java code?

    class char_increment 
    {
        public static void main(String args[]) 
        {
            char c1 = 'D';
            char c2 = 84;
            c2++;
            c1++;
            System.out.println(c1 + " "  + c2);
        } 
    }

a) E U
b) U E
c) V E
d) U F
Answer: a
7. What will be the output of the following Java code?

    class conversion 
    {
        public static void main(String args[]) 
        {
            double a = 295.04;
            int  b = 300;
            byte c = (byte) a;
            byte d = (byte) b;
            System.out.println(c + " "  + d);
        } 
    }

a) 38 43
b) 39 44
c) 295 300
d) 295.04 300
Answer: b
8. What will be the output of the following Java code?

    class A 
    {
        final public int calculate(int a, int b) { return 1; } 
    } 
    class B extends A 
    { 
        public int calculate(int a, int b) { return 2; } 
    } 
     public class output 
     {
        public static void main(String args[]) 
        { 
            B object = new B(); 
            System.out.print("b is " + b.calculate(0, 1));  
        } 
    }

a) b is : 2
b) b is : 1
c) Compilation Error
d) An exception is thrown at runtime
Answer: c
9. What will be the output of the following Java program, if we run as “java main_arguments 1 2 3”?

    class main_arguments 
    {
        public static void main(String [] args) 
        {
            String [][] argument = new String[2][2];
            int x;
            argument[0] = args;
            x = argument[0].length;
            for (int y = 0; y < x; y++) 
                System.out.print(" " + argument[0][y]);              
        }
    }

a) 1 1
b) 1 0
c) 1 0 3
d) 1 2 3
Answer: d
10. What will be the output of the following Java program?

    class c 
    {    
        public void main( String[] args ) 
        {  
            System.out.println( "Hello" + args[0] ); 
        } 
    }

a) Hello c
b) Hello
c) Hello world
d) Runtime Error
Answer: d

Java Questions & Answers – Arrays

1. Which of these operators is used to allocate memory to array variable in Java?
a) malloc
b) alloc
c) new
d) new malloc
Answer: c
2. Which of these is an incorrect array declaration?
a) int arr[] = new int[5]
b) int [] arr = new int[5]
c) int arr[] = new int[5]
d) int arr[] = int [5] new
Answer: d
3. What will be the output of the following Java code?

    int arr[] = new int [5];
    System.out.print(arr);

a) 0
b) value stored in arr[0]
c) 00000
d) Class hashcode in hexadecimal form
Answer: d
4. Which of these is an incorrect Statement?
a) It is necessary to use new operator to initialize an array
b) Array can be initialized using comma separated expressions surrounded by curly braces
c) Array can be initialized when they are declared
d) None of the mentioned
Answer: a
5. Which of these is necessary to specify at time of array initialization?
a) Row
b) Column
c) Both Row and Column
d) None of the mentioned
Answer: a
6. What will be the output of the following Java code?

    class array_output 
    {
        public static void main(String args[]) 
        {
            int array_variable [] = new int[10];
	    for (int i = 0; i < 10; ++i) 
            {
                array_variable[i] = i;
                System.out.print(array_variable[i] + " ");
                i++;
            }
        } 
    }

a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
Answer: a
7. What will be the output of the following Java code?

    class multidimention_array 
    {
        public static void main(String args[])
        {
            int arr[][] = new int[3][];
            arr[0] = new int[1];
            arr[1] = new int[2];
            arr[2] = new int[3];               
	    int sum = 0;
	    for (int i = 0; i < 3; ++i) 
	        for (int j = 0; j < i + 1; ++j)
                    arr[i][j] = j + 1;
	    for (int i = 0; i < 3; ++i) 
	        for (int j = 0; j < i + 1; ++j)
                    sum + = arr[i][j];
	    System.out.print(sum); 	
        } 
    }

a) 11
b) 10
c) 13
d) 14
Answer: b
8. What will be the output of the following Java code?

    class evaluate 
    {
        public static void main(String args[]) 
            {
	        int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
	        int n = 6;
                n = arr[arr[n] / 2];
	        System.out.println(arr[n] / 2);
            } 
    }

a) 3
b) 0
c) 6
d) 1
Answer: d
9. What will be the output of the following Java code?

    class array_output 
    {
        public static void main(String args[]) 
        {
            char array_variable [] = new char[10];
	    for (int i = 0; i < 10; ++i) 
            {
                array_variable[i] = 'i';
                System.out.print(array_variable[i] + "");
            }
        } 
    }

a) 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 3 4 5 6 7 8 9 10
c) i j k l m n o p q r
d) i i i i i i i i i i
Answer: d
10. What will be the output of the following Java code?

    class array_output 
    {
        public static void main(String args[]) 
        {
            int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
            int sum = 0;
            for (int i = 0; i < 3; ++i)
                for (int j = 0; j <  3 ; ++j)
                    sum = sum + array_variable[i][j];
            System.out.print(sum / 5);
        } 
    }

a) 8
b) 9
c) 10
d) 11
Answer: b

* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first.