Programming MCQs

MCQs on Java Programming With Answers set-11

Pinterest LinkedIn Tumblr

This set of MCQs on java programming includes a collection of multiple-choice questions on the fundamentals of java programming language with answers. It includes MCQ on the types of variables and keywords, examples of control flow statements such as if statement and switch statement, and the ways of defining the class in java programming along with their answers.

1. If m and n are int-type variables, what will be the result of the expression m%n when m=-14 and n=-3?
A) 4
B) 2
C) -2
D) -4

2. Consider the following code

if(number>=0)
    if(number>0)
    system.out.println("Number is positive");
else
    system.out.println("Number is negative");

What will be the output if the number is equal to 0?
A) Number is negative
B) Number is positive
C) Both A and B
D) None of the above

3. Consider the following code:

char c='a';
switch (c)
{
     case 'a';
     system.out.println("A");
     case 'b'; 
     system.out.println("B");
     default; 
system.out.println("C");
} 

For this code, which of the following statement is true?
A) The output will be A
B) The output will be A followed by B
C) The output will be A, followed by B, and then followed by C
D) Code is illegal and therefore will not compile

4. Consider the following class definition.

class Student extends String

  {
  }

What happens when we try to compile this class?
A) Will not compile because the class body is not defined
B) Will not compile because the class is not declared public.
C) Will not compile because the string is abstract.
D) Will not compile because the string is final.

5. What is wrong with the following class definitions?

abstract class print
{
  abstract show();
}
class Display extends Print
{
}

A) Nothing is wrong
B) Wrong. Method show() should have a return type
C) Wrong. Method show() is not implemented in Display
D) Wrong. The display does not contain any numbers.

Read Also: Solved MCQ on Fundamental of Java Language

6. What is the error in the following class definitions?

abstract class XY
{
  abstract sum(int x, int y){    }
}

A) The class header is not defined properly
B) The constructor is not defined
C) The method is not defined properly
D) No error.

7. Which of the following statements is true?
i) We cannot use abstract classes to instantiate objects directly.
ii) The abstract methods of an abstract class must be defined in its subclass.
iii) We cannot declare abstract constructors.
iv) We may declare abstract static methods.
A) Line i only
B) Line ii only
C) Line i and ii only
D) Line i, ii and iii only

8. We would like to make a member of a class visible in all subclasses regardless of what package they are in. Which one of the following keywords would archive this?
A) Private
B) Protected
C) Public
D) Private Protected

9. The use of a protected keyword to a member in a class will restrict its visibility as follows:
A) Visible only in the class and its subclass in the same package.
B) Visible only inside the same package.
C) Visible in all classes in the same package and subclasses in other packages
D) Visible only in the class where it is declared.

10. Consider the following code:

interface Area
{
  float compute (float x, float y);
}
class Room implements Area
{
  float compute (float x, float y)

      {
  return(x&y);
 }
 }

What is wrong with the code?
A) The interface definition is incomplete
B) Method compute() in interface Area should be declared public
C) Method compute() in class Room should be declared public
D) All the above

Answers:

1. If m and n are int-type variables, what will be the result of the expression m%n when m=-14 and n=-3?
C) -2

2. Consider the following code

if(number>=0)
    if(number>0)
    system.out.println("Number is positive");
else
    system.out.println("Number is negative");

What will be the output if the number is equal to 0?
A) Number is negative

3. Consider the following code:

char c='a';
switch (c)
{
     case 'a';
     system.out.println("A");
     case 'b'; 
     system.out.println("B");
     default; 
     system.out.println("C");
} 

For this code, which of the following statement is true?
B) The output will be A followed by B

4. Consider the following class definition.

class Student extends String
  {
  }

What happens when we try to compile this class?
D) Will not compile because the string is final.

5. What is wrong with the following class definitions?

abstract class print
{
    abstract show();
}
class Display extends Print
{
}

C) Wrong. Method show() is not implemented in Display

6. What is the error in the following class definitions?

abstract class XY
{
   abstract sum(int x, int y){    }
}

C) The method is not defined properly

7. Which of the following statements is true?
i) We cannot use abstract classes to instantiate objects directly.
ii) The abstract methods of an abstract class must be defined in its subclass.
iii) We cannot declare abstract constructors.
iv) We may declare abstract static methods.
D) Line i, ii and iii only

8. We would like to make a member of a class visible in all subclasses regardless of what package they are in. Which one of the following keywords would archive this?
D) Private Protected

9. The use of a protected keyword to a member in a class will restrict its visibility as follows:
C) Visible in all classes in the same package and subclasses in other packages

10. Consider the following code:

interface Area
{
     float compute (float x, float y);
}
class Room implements Area
{
    float compute (float x, float y)
      {
           return(x&y);
      }
 }

What is wrong with the code?
C) Method compute() in class Room should be declared public

Read Next: MCQ on Java Programming Language Fundamental set-12

Author

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 8 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Comments are closed.