Java MCQ with Answer by Abdallah Ibrahim PDF
Document Details
Abdallah Ibrahim
Tags
Summary
This document contains multiple choice questions (MCQs) focused on Java programming. The questions cover various topics including basic programming concepts, operators, data types, and methodology. Questions also cover important topics such as program design, object-oriented programming, and program execution.
Full Transcript
Chapter 1: Introduction to Computers, Programs, and Java 1.What is the brain of a computer? Memory CPU Hard disk Monitor 2.Which of the following is NOT a storage device? Hard disk Floppy disk Monitor Tape drive 3.What is the purpose of an operating system? To write computer programs.○ To translate...
Chapter 1: Introduction to Computers, Programs, and Java 1.What is the brain of a computer? Memory CPU Hard disk Monitor 2.Which of the following is NOT a storage device? Hard disk Floppy disk Monitor Tape drive 3.What is the purpose of an operating system? To write computer programs.○ To translate source code into machine code. To manage and control a computer's activities To connect to the Internet. 4.Which of the following is a high-level programming language? Machine language Assembly language Java Binary code 5.What is the name of the program that converts assembly language programs into machine code? Compiler Interpreter Assembler Translator 6.What is the extension for Java source code files?.exe.bin.class.java 7.What is bytecode? Machine code that can be directly executed by a computer. Java Virtual Machine code that is machine-independent. A type of high-level programming language. A type of storage device. 8.Which of the following is NOT a characteristic of Java? Object-oriented Secure Portable Difficult to learn 9.What does the term "architecture-neutral" mean in the context of Java? Java programs can run on any platform without being recompiled. Java programs are immune to security threats. Java programs can be easily distributed across a network. Java programs are very efficient and perform well. 10.What is the purpose of an IDE (Integrated Development Environment)? To write Java programs. To compile Java programs. To run Java programs. All of the above. 11.What is the output of the following Java code? 1. public class Welcome { 2. public static void main(String[] args) { 3. System.out.println("Welcome to Java!"); 4. } 5. } Welcome Welcome to Java! "Welcome to Java!" Nothing 12.Which of the following is a valid Java identifier? 123radius _radius radius-area public 13.What is the purpose of the main method in a Java program? To define the class name. To specify the starting point of program execution. To declare variables. To print output to the console. 14.What is a statement terminator in Java? A period (.) A comma (,) A semicolon (;) A colon (:) 15.What is the purpose of comments in Java code? To make the code more efficient. To explain the code and make it more readable. To change the behavior of the program. To declare variables. 16.Which of the following is a syntax error? Dividing an integer by zero. Using a variable that has not been declared. Missing a semicolon at the end of a statement. Calculating the area of a circle with a negative radius. 17.What is a runtime error? An error that is detected by the compiler. An error that occurs during program execution. An error that results in incorrect program output. An error that prevents the program from compiling. 18.What is a logic error? An error that is detected by the compiler. An error that occurs during program execution. An error that results in incorrect program output. An error that prevents the program from compiling. Chapter 2: Elementary Programming 19.What is the first step in the software development process? System analysis System design Implementation Requirement specification 20.What is IPO in the context of system analysis and design? Input, Process, Output Internet Protocol Output Internal Program Organization Integrated Programming Objects 21.What is the purpose of the implementation phase in the software development process? To gather requirements from the user. To analyze the business process. To design the system's components. To translate the system design into programs. 22.What is the purpose of testing in the software development process? To design the user interface. To ensure that the code meets the requirements specification and to find bugs. To write the program code. To deploy the software to the users. 23.Which of the following is a valid variable declaration in Java? int x; x = 1; int 1x; integer x; 24.What is the value of x after the following Java code is executed? 19. int x; 20. x = 1; 0 1 Undefined null 25.What is a named constant in Java? A variable whose value can be changed during program execution. A variable whose value cannot be changed after it is initialized. A keyword that has a specific meaning to the compiler. A special symbol that represents an operation. 26.What is the convention for naming classes in Java? Use lowercase. Use uppercase. Capitalize the first letter of each word. Use underscores to separate words. 27.What is the smallest numerical data type in Java? short int byte long 28.What is the largest numerical data type in Java? int float double long 29.What is the result of the following Java expression? 19. 5 / 2 2.5 2 3 2.0 30.What is the result of the following Java expression? 19. 5 % 2 2.5 2 1 0 31.What is the purpose of the Scanner class in Java? To perform mathematical calculations. To read input from the console. To write output to the console. To declare variables. 32.What is the output of the following Java code? 19. System.out.println(Math.pow(2, 3)); 6 8.0 9 16 33.What is a number literal in Java? A variable that stores a numerical value. A constant value that appears directly in the program. A mathematical operation. A keyword that represents a data type. 34.What is the data type of the literal 5.0 in Java? int float double long 35.What is the purpose of the System.currentTimeMillis() method? To calculate the area of a circle. To convert Fahrenheit to Celsius. To obtain the current time in milliseconds since the Unix epoch. To generate random numbers. 36.What is the difference between the ++i and i++ operators? ++i increments the value of i before it is used in the expression, while i++ increments the value of i after it is used. ++i increments the value of i by 2, while i++ increments the value of i by 1. ++i is used for pre-incrementing, while i++ is used for post-incrementing. Both operators have the same effect. 37.What is type casting in Java? Converting a variable from one data type to another. Assigning a value to a variable. Declaring a variable with a specific data type. Performing mathematical operations on variables. 38.What is the result of the following Java code? 19. int i = (int)3.9; 20. System.out.println(i); 3.9 3 4 An error Chapter 3: Selections 39.What is the data type of a variable that can store the values true or false? int double String boolean 40.Which of the following is a relational operator in Java? + * > = 41.What is the purpose of an if statement? To execute a block of code repeatedly. To execute a block of code only if a certain condition is true. To declare variables. To define methods. 42.What is the purpose of an else clause in an if statement? To specify the code to be executed if the if condition is true. To specify the code to be executed if the if condition is false. To define a loop. To declare a variable. 43.What is a nested if statement? An if statement that contains another if statement inside it. An if statement that has multiple else clauses. An if statement that uses logical operators. An if statement that is used in a loop. 44.What is the purpose of the break statement in a switch statement? To exit the program. To skip the current iteration of a loop. To exit the switch statement. To declare a variable. 45.What is the purpose of the default case in a switch statement? To specify the code to be executed if none of the other cases match. To specify the code to be executed if all of the other cases match. To declare a variable. To define a method. 46.What is a conditional expression in Java? A shorthand way of writing an if-else statement. A logical expression that uses relational operators. A mathematical expression that uses arithmetic operators. A string expression that uses concatenation operators. 47.What is the operator precedence in Java? The order in which operators are evaluated in an expression. The associativity of operators in an expression. The data type of the operands in an expression. The number of operands in an expression. 48.What is the operator associativity in Java? The order in which operators of the same precedence are evaluated. The precedence of operators in an expression. The data type of the operands in an expression. The number of operands in an expression. Chapter 4: Mathematical Functions, Characters, and Strings 49.What is the purpose of the Math class in Java? To read input from the console. To write output to the console. To perform mathematical calculations. To manipulate strings. 50.What is the value of Math.PI? 3.14 3.1415 3.14159265358979339 3.1416 51.What is the purpose of the Math.sin() method? To calculate the sine of an angle in degrees. To calculate the sine of an angle in radians. To calculate the cosine of an angle in degrees. To calculate the cosine of an angle in radians. 52.What is the purpose of the Math.pow() method? To calculate the square root of a number. To raise a number to a power. To calculate the absolute value of a number. To generate a random number. 53.What is the purpose of the Math.ceil() method? To round a number down to the nearest integer. To round a number up to the nearest integer. To round a number to the nearest even integer. To calculate the absolute value of a number. 54.What is the purpose of the Math.random() method? To generate a random integer between 0 and 9. To generate a random integer between 1 and 10. To generate a random double value between 0.0 and 1.0 To generate a random boolean value. 55.What is the data type of a variable that can store a single character? int double String char 56.What is Unicode? A 8-bit character encoding scheme. A 16-bit character encoding scheme. A 32-bit character encoding scheme. A 64-bit character encoding scheme. 57.What is an escape sequence in Java? A sequence of characters that represents a special character. A sequence of characters that represents a Unicode character. A sequence of characters that represents a string literal. A sequence of characters that represents a numerical value. 58.What is the purpose of the Character.isDigit() method? To check if a character is a letter. To check if a character is a digit. To convert a character to uppercase. To convert a character to lowercase. 59.What is the data type of a variable that can store a sequence of characters? int double char String 60.What is the purpose of the String.length() method? To concatenate two strings. To convert a string to uppercase. To return the number of characters in a string. To find the index of a character in a string. 61.What is the purpose of the String.charAt() method? To concatenate two strings. To convert a string to uppercase. To return the number of characters in a string. To return the character at a specific index in a string. 62.What is the purpose of the String.concat() method? To concatenate two strings. To convert a string to uppercase. To return the number of characters in a string. To find the index of a character in a string. 63.What is the purpose of the String.toUpperCase() method? To concatenate two strings. To convert a string to uppercase To return the number of characters in a string. To find the index of a character in a string. 64.What is the purpose of the String.toLowerCase() method? To concatenate two strings. To convert a string to uppercase. To return the number of characters in a string. To convert a string to lowercase. 65.What is the purpose of the String.trim() method? To concatenate two strings. To convert a string to uppercase. To return the number of characters in a string. To remove leading and trailing whitespace from a string. 66.What is the purpose of the String.equals() method? To compare two strings for equality, ignoring case. To compare two strings for equality, considering case. To concatenate two strings. To find the index of a character in a string. 67.What is the purpose of the String.equalsIgnoreCase() method? To compare two strings for equality, ignoring case To compare two strings for equality, considering case. To concatenate two strings. To find the index of a character in a string. 68.What is the purpose of the String.compareTo() method? To compare two strings for equality. To concatenate two strings. To compare two strings lexicographically. To find the index of a character in a string. 69.What is the purpose of the String.substring() method? To concatenate two strings. To convert a string to uppercase. To return the number of characters in a string. To extract a portion of a string 70.What is the purpose of the String.indexOf() method? To concatenate two strings. To convert a string to uppercase. To return the number of characters in a string. To find the index of a character or substring in a string. 71.What is the purpose of the Integer.parseInt() method? To convert an integer to a string. To convert a string to an integer. To concatenate two strings. To find the index of a character in a string. 72.What is the purpose of the Double.parseDouble() method? To convert a double to a string. To convert a string to a double. To concatenate two strings. To find the index of a character in a string. 73.What is the purpose of the System.out.printf() method? To read input from the console. To format and print output to the console. To perform mathematical calculations. To manipulate strings. 74.What is a format specifier in Java? A special character that represents a data type. A code that specifies how a value should be formatted. A keyword that has a specific meaning to the compiler. A variable that stores a value. General Java Concepts 75. What is the correct syntax for declaring a variable named age of type int and assigning it the value 25? int age = 25; age = 25; int age 25; 25 = age; 76. Which of the following is a valid comment in Java? // This is a comment # This is a comment Both a and b Problem-Solving 77.Write a Java program to calculate the sum of two numbers entered by the user. import java.util.Scanner; public class SumOfTwoNumbers { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the first number: "); int number1 = input.nextInt(); System.out.print("Enter the second number: "); int number2 = input.nextInt(); int sum = number1 + number2; System.out.println("The sum of " + number1 + " and " + number2 + " is " + sum); } } 78.Write a Java program to check if a number is even or odd. import java.util.Scanner; public class EvenOdd { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number: "); int number = input.nextInt(); if (number % 2 == 0) { System.out.println(number + " is even."); } else { System.out.println(number + " is odd."); } } } 79.Write a Java program to find the largest of three numbers. import java.util.Scanner; public class LargestOfThree { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the first number: "); int number1 = input.nextInt(); System.out.print("Enter the second number: "); int number2 = input.nextInt(); System.out.print("Enter the third number: "); int number3 = input.nextInt(); int largest = number1; if (number2 > largest) { largest = number2; } if (number3 > largest) { largest = number3; } System.out.println("The largest number is " + largest); } } 80.Write a Java program to print the multiplication table of a number entered by the user. import java.util.Scanner; public class MultiplicationTable { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number: "); int number = input.nextInt(); for (int i = 1; i