All Exams  >   EmSAT Achieve  >   Java for EmSAT Achieve  >   MCQ Questions

Starting with Java MCQs for EmSAT Achieve Exam

It covers all Important Questions with answers on Starting with Java for the EmSAT Achieve exam. The questions are based on important topics. Details about the questions:
  • Topic: Starting with Java
  • Type of Questions: MCQs with solutions
  • Number of Questions: 40
  • You can attempt them on EduRev to score high in EmSAT Achieve exam.

Which of the following is not a valid identifier in Java?
  • a)
    myVariable
  • b)
    _variable
  • c)
    123abc
  • d)
    $price
Correct answer is option 'C'. Can you explain this answer?

Invalid Identifier in Java

An identifier is a name given to a variable, method, class, or other programming elements in Java. It is used to uniquely identify these elements within a program. Identifiers in Java must follow certain rules and conventions.

In the given options, the invalid identifier in Java is:

c) 123abcd

Explanation:

In Java, an identifier must adhere to the following rules:

1. It must start with a letter (a-z or A-Z), underscore (_), or a dollar sign ($).
2. After the first character, it can contain letters, digits, underscores, or dollar signs.
3. It cannot contain spaces or any special characters except underscore (_) or a dollar sign ($).
4. It cannot be a reserved word or keyword.

Let's analyze each option:

a) myVariable
- This is a valid identifier in Java as it starts with a letter and contains only letters.

b) _variable
- This is also a valid identifier in Java as it starts with an underscore and contains only letters.

c) 123abcd
- This is not a valid identifier in Java because it starts with a digit (number) instead of a letter, underscore, or dollar sign.

d) $price
- This is a valid identifier in Java as it starts with a dollar sign and contains only letters.

Conclusion:

The option c) 123abcd is not a valid identifier in Java as it violates the rule that an identifier must start with a letter, underscore, or a dollar sign.

Which of the following is NOT a primitive data type in Java?
  • a)
    int
  • b)
    double
  • c)
    string
  • d)
    boolean
Correct answer is option 'C'. Can you explain this answer?

Anjali Iyer answered
The correct answer is option 'C' - String.

In Java, a primitive data type represents a basic type of data that is not an object. These data types are predefined by the language and are used to declare variables. There are eight primitive data types in Java:

a) int: Represents integer values, such as 1, 2, -3, etc. It is used to store whole numbers without any fractional part.

b) double: Represents floating-point numbers, such as 3.14, 2.718, etc. It is used to store numbers with decimal places.

c) String: This is NOT a primitive data type in Java. Instead, it is a class from the Java standard library that represents a sequence of characters. Strings are used to store textual data, such as names, sentences, etc.

d) boolean: Represents a boolean value, which can be either true or false. It is used for logical operations and to determine the flow of control in programs.

The String class in Java is not a primitive data type because it is an object. It is part of the Java standard library and provides various methods to manipulate and work with strings. Although strings are commonly used in Java programs, they are not considered primitive because they are instances of a class.

While primitive data types are simple and have a fixed size, objects like the String class can have variable lengths and provide additional functionality through their methods.

To declare and use a String variable in Java, you need to use the class name followed by the variable name, like:
String name = "John";

In summary, the String data type is NOT a primitive data type in Java. It is an object from the String class in the Java standard library that represents a sequence of characters.

What is the output of the following code?
public class Main {
    public static void main(String[] args) {
        int x = 5;
        int y = x++;
        System.out.println(y);
    }
}
  • a)
    4
  • b)
    5
  • c)
    6
  • d)
    Compilation Error
Correct answer is option 'B'. Can you explain this answer?

Sanchita Yadav answered
The output of the given code is '5'.

Explanation:
- The code starts with the declaration of a variable 'x' and assigns it the value of 5.
- Then, another variable 'y' is declared and assigned the value of 'x'.
- Finally, the value of 'y' is printed using the System.out.println() method.

Let's understand the code execution step by step:
1. Variable 'x' is declared and assigned the value of 5.
2. Variable 'y' is declared and assigned the value of 'x'. This means that the value of 'y' will be 5 since 'x' is also 5.
3. The value of 'y' is printed using the System.out.println() method. This will output '5' to the console.

In the given code, there are no compilation errors. The code is simple and straightforward, assigning the value of 'x' to 'y' and then printing the value of 'y'. As both 'x' and 'y' have the same value of 5, the output will be '5'.

Therefore, the correct answer is option 'B' - 5.

What will be the output of the following code?
public class Test {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        changeArray(numbers);
        System.out.println(numbers[0]);
    }
    
    public static void changeArray(int[] arr) {
        arr[0] = 10;
    }
}
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    10
Correct answer is option 'D'. Can you explain this answer?

Explanation:
The given code defines a class named "Test" with a main method. Inside the main method, an integer array named "numbers" is declared and initialized with the values {1, 2, 3, 4, 5}. Then, the method "changeArray" is called with the array "numbers" as the argument. Finally, the value at index 0 of the array "numbers" is printed.

Step-by-step execution:
1. The array "numbers" is declared and initialized with the values {1, 2, 3, 4, 5}.
2. The "changeArray" method is called with the array "numbers" as the argument.
3. Inside the "changeArray" method, the value at index 0 of the array "arr" is changed to 10.
4. Control returns to the main method.
5. The value at index 0 of the array "numbers" is printed, which is now 10.

Explanation:
The array "numbers" is passed as an argument to the "changeArray" method. In Java, arrays are passed by reference, which means that any changes made to the array inside the method will affect the original array.

Conclusion:
Therefore, when the value at index 0 of the array "arr" is changed to 10 inside the "changeArray" method, it also changes the value at index 0 of the array "numbers" in the main method. As a result, the output of the code is 10.

What is the output of the following code snippet?
int a = 5;
int b = 7;
int c = a + b;
System.out.println(c);
  • a)
    12
  • b)
    57
  • c)
    5+7
  • d)
    Compilation Error
Correct answer is option 'A'. Can you explain this answer?

The output of the code snippet is 12.

Explanation:
- The code snippet initializes three variables: `a` with a value of 5, `b` with a value of 7, and `c` without any value yet.
- The next line assigns the value of `a` multiplied by `b` to `c`. The multiplication operator (*) is used to perform the multiplication operation.
- Therefore, the value of `c` is 5 multiplied by 7, which is 35.
- Finally, the `System.out.println()` statement is used to print the value of `c`, which is 35, to the console.

Therefore, the correct answer is option 'A' (12)

What is the output of the following code?
public class Main {
    public static void main(String[] args) {
        int x = 5;
        int y = 10;
        int result = (x > y) ? x++ : y--;
        System.out.println(result);
    }
}
  • a)
    5
  • b)
    6
  • c)
    9
  • d)
    Compilation Error
Correct answer is option 'A'. Can you explain this answer?

The code provided is incomplete and contains a syntax error. The line "int result = (x" is missing the closing parenthesis and the operation to be performed. Without the complete code, it is not possible to determine the output.

What is the output of the following code?
int x = 6;
int y = 3;
System.out.println(x / y);
  • a)
    2
  • b)
    2.0
  • c)
    2.5
  • d)
    Compilation Error
Correct answer is option 'A'. Can you explain this answer?

KnowIT answered
When both the operands of the division operator (/) are integers, the result will also be an integer. In this case, 6 divided by 3 is equal to 2, so the output will be 2.

Which of the following statements is true about Java?
  • a)
    Java is a low-level programming language.
  • b)
    Java is platform-dependent.
  • c)
    Java is an object-oriented programming language.
  • d)
    Java supports multiple inheritance.
Correct answer is option 'C'. Can you explain this answer?

Java is an object-oriented programming language.

Explanation:
Java is a high-level, object-oriented programming language that was developed by Sun Microsystems and released in 1995. It was designed to be platform-independent, meaning that Java programs can run on any operating system or platform as long as a Java Virtual Machine (JVM) is installed.

Object-Oriented Programming:
Java is primarily an object-oriented programming (OOP) language. This means that it follows the principles of OOP, such as encapsulation, inheritance, and polymorphism. In Java, everything is treated as an object, including variables, functions, and even the programs themselves. This allows for modularity, reusability, and easier maintenance of code.

Platform Independence:
One of the key features of Java is its platform independence. Java programs are compiled into bytecode, which is a platform-neutral format. This bytecode can then be executed on any system that has a JVM, making Java programs highly portable. This is in contrast to platform-dependent languages, where the compiled code is specific to a particular operating system or hardware architecture.

Java Virtual Machine:
The Java Virtual Machine (JVM) is an essential component of the Java platform. It is responsible for executing the bytecode generated by the Java compiler. The JVM acts as an interpreter, translating the bytecode into machine code that can be understood and executed by the underlying operating system. This allows Java programs to run on any device or platform that has a compatible JVM installed.

Support for Multiple Inheritance:
Java does not support multiple inheritance, which is the ability for a class to inherit from more than one superclass. This was a deliberate design choice to keep the language simple and prevent certain programming issues that can arise from multiple inheritance. Instead, Java supports single inheritance, where a class can inherit from only one superclass, but it can implement multiple interfaces.

In conclusion, the statement "Java is an object-oriented programming language" is true. Java's object-oriented nature, platform independence, and support for single inheritance are some of the key reasons for its popularity and widespread use in various domains, including web development, mobile app development, and enterprise software development.

What is the output of the following code snippet?
int x = 5;
int y = 2;
int result = x / y;
System.out.println(result * 2);
  • a)
    5
  • b)
    4
  • c)
    10
  • d)
    Compilation Error
Correct answer is option 'C'. Can you explain this answer?

Code Explanation:
The given code snippet performs a mathematical operation using the division operator (/) to calculate the result of x divided by y. The value of x is 5 and the value of y is 2. The result of the division operation is then stored in the variable 'result'. Finally, the code prints out the value of 'result' multiplied by 2 using the System.out.println() method.

Code Execution:
To understand the output of the code, let's go through the execution step by step:

1. The variable 'x' is assigned the value 5.
2. The variable 'y' is assigned the value 2.
3. The division operation 'x / y' is performed. Since both 'x' and 'y' are of type 'int', the division operation returns the quotient of the division, which is 2.5.
4. The result of the division operation, 2.5, is stored in the variable 'result'.
5. The code then prints out the value of 'result' multiplied by 2 using the System.out.println() method. The value of 'result' is 2.5, so when multiplied by 2, the output will be 5.0.

Output:
The output of the code snippet will be:
5.0

Explanation:
The answer to this question is option 'C', which is 10. However, this is incorrect. The correct answer is option 'A', which is 5.0.

The reason for this is that when performing arithmetic operations in Java, if both operands are integers, the result will be an integer. In the given code snippet, 'x' and 'y' are both integers, so the division operation 'x / y' will yield an integer result. In this case, the result is 2, since 5 divided by 2 is 2 with a remainder of 1.

When the code prints out the value of 'result' multiplied by 2, it will print out 5.0, not 10.0, because the result is an integer, not a decimal number. However, when the integer 5 is multiplied by 2, it is automatically promoted to a decimal number, resulting in 5.0.

Therefore, the correct output of the code snippet is 5.0.

What is the purpose of the 'this' keyword in Java?
  • a)
    It is used to refer to the current instance of a class.
  • b)
    It is used to create an object of a class.
  • c)
    It is used to access static variables and methods.
  • d)
    It is used to access superclass members.
Correct answer is option 'A'. Can you explain this answer?

KnowIT answered
The 'this' keyword in Java is used to refer to the current instance of a class. It is typically used to distinguish between instance variables and method parameters/ local variables with the same name.

What is the output of the following code?
public class Main {
    public static void main(String[] args) {
        int x = 10;
        int y = 5;
        int result = x / y;
        System.out.println(result);
    }
}
  • a)
    2
  • b)
    2.0
  • c)
    2.5
  • d)
    Compilation Error
Correct answer is option 'A'. Can you explain this answer?

Given code:

The given code is a Java program that performs a division operation and prints the result.

```
public class Main {
public static void main(String[] args) {
int x = 10;
int y = 5;
int result = x / y;
System.out.println(result);
}
}
```

Explanation:

1. The code starts with a class named `Main`, which contains the `main` method.
2. Inside the `main` method, two integer variables `x` and `y` are declared and assigned the values 10 and 5 respectively.
3. The division operation `x / y` is performed and the result is stored in the variable `result`.
4. The result is then printed using the `System.out.println` statement.

Output:

The output of the program will be the value of the variable `result`, which is the result of the division operation `x / y`.

In this case, since both `x` and `y` are integers, the division operation is an integer division. The result of the division is the quotient of the division, which is the largest integer that is less than or equal to the exact quotient.

In this case, the exact quotient of 10 divided by 5 is 2. Since the division is an integer division, the result will be the largest integer that is less than or equal to 2, which is 2.

Therefore, the output of the program will be:

2

Chapter doubts & questions for Starting with Java - Java for EmSAT Achieve 2026 is part of EmSAT Achieve exam preparation. The chapters have been prepared according to the EmSAT Achieve exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for EmSAT Achieve 2026 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Starting with Java - Java for EmSAT Achieve in English & Hindi are available as part of EmSAT Achieve exam. Download more important topics, notes, lectures and mock test series for EmSAT Achieve Exam by signing up for free.

Java for EmSAT Achieve

60 videos|37 docs|12 tests

Top Courses EmSAT Achieve