GC 101 Programming Essentials - Java Programming Questions PDF

Summary

This document is a question bank for Programming Essentials (GC 101) focusing on Java concepts. The questions cover a wide range of topics, including Java programming, data types, identifiers, control structures, and more. The questions are good for studying and practical application, the use of algorithms and translating programming languages is also discussed.

Full Transcript

Question Bank GC 101 Programming Essentials Question 1: What is the set of instructions that tells a computer to perform specific tasks called? A) Hardware B) Operating system C) Computer program...

Question Bank GC 101 Programming Essentials Question 1: What is the set of instructions that tells a computer to perform specific tasks called? A) Hardware B) Operating system C) Computer program D) Network Answer: C) Computer program Question 2: Which of the following is required for a computer to perform a specific task? A) Data B) Instructions C) Memory D) Power supply Answer: B) Instructions Question 3: A computer program is written in a specific language understood by the computer. What is this language called? A) Binary code B) Assembly language C) Programming language D) Machine code Answer: C) Programming language Question 4: Which component of a computer executes the instructions given in a computer program? A) Monitor B) CPU (Central Processing Unit) C) RAM (Random Access Memory) D) Hard disk Answer: B) CPU (Central Processing Unit) Question 5: A program that translates high-level programming languages into machine language so the computer can understand it is called: A) Compiler B) Operating system C) Application D) Database Answer: A) Compiler Question 6: What is the set of instructions that tells a computer to perform specific tasks called? A) Hardware B) Operating system C) Computer program D) Network Answer: C) Computer program Question 7: Which of the following is required for a computer to perform a specific task? A) Data B) Instructions C) Memory D) Power supply Answer: B) Instructions Question 8: A computer program is written in a specific language understood by the computer. What is this language called? A) Binary code B) Assembly language C) Programming language D) Machine code Answer: C) Programming language Question 9: Which component of a computer executes the instructions given in a computer program? A) Monitor B) CPU (Central Processing Unit) C) RAM (Random Access Memory) D) Hard disk Answer: B) CPU (Central Processing Unit) Question 10: A program that translates high-level programming languages into machine language so the computer can understand it is called: A) Compiler B) Operating system C) Application D) Database Answer: A) Compiler Question 11: What is an algorithm? A) A programming language B) A machine instruction C) A finite sequence of instructions to solve a problem D) A type of hardware component Answer: C) A finite sequence of instructions to solve a problem Question 12: What is the first step in solving a problem using an algorithm? A) Code the program B) Plan the problem solution C) Define the problem D) Compile the program Answer: C) Define the problem Question 13: Which step comes immediately after planning the problem solution in the development of a program? A) Run the program B) Compile the program C) Code the program D) Test and debug the program Answer: C) Code the program Question 14: What does compiling a program involve? A) Writing the program code B) Translating the program code into machine language C) Testing the program for errors D) Running the program Answer: B) Translating the program code into machine language Question 15: What is the purpose of the "Test and Debug" step in the program development process? A) To plan the problem solution B) To translate the program into machine code C) To identify and correct errors in the program D) To define the problem Answer: C) To identify and correct errors in the program Question 16: Which of the following is the correct sequence of steps in program development? A) Compile, Code, Run, Test and Debug B) Define, Plan, Code, Compile, Run, Test and Debug C) Plan, Define, Run, Compile, Code, Test and Debug D) Code, Define, Compile, Plan, Run, Test and Debug Answer: B) Define, Plan, Code, Compile, Run, Test and Debug Question 12: What is the process of writing a program in a formal language known as? A) Debugging B) Coding C) Compiling D) Testing Answer: B) Coding Question 18: What is the program that is written by translating algorithm steps into programming language statements called? A) Machine code B) Source code C) Executable file D) Compiler Answer: B) Source code Question 19: What file extension is typically used for Java source code? A).exe B).cpp C).java D).py Answer: C).java Question 20: Why can't computers directly understand programs written in languages such as C++ and Java? A) They lack the necessary hardware B) They only understand machine code C) They can only run Python D) They require more memory Answer: B) They only understand machine code Question 21: What is the role of a compiler in programming? A) To execute the program B) To edit the program C) To translate programming language statements into machine code D) To debug the program Answer: C) To translate programming language statements into machine code Question 22: Which of the following is true about machine code? A) It is written in a high-level language B) It is a human-readable code C) It is the only code that computers can directly execute D) It is used only for testing purposes Answer: C) It is the only code that computers can directly execute Question 23: When saving a Java program, what file extension should be used? A).exe B).cpp C).java D).class Answer: C).java Question 24: What is the command used to compile a Java program? A) run java B) java C) javac.java D) compile java Answer: C) javac.java Question 25: After compiling a Java program, the compiler generates a file with which extension? A).java B).exe C).cpp D).class Answer: D).class Question 26: What command is used to run a compiled Java program? A) run java B) java C) javac.class D) compile Answer: B) java Question 27: Which of the following is true about the process of compiling a Java program? A) It directly converts source code into machine code B) It translates the source code into bytecode that can be run on the Java Virtual Machine (JVM) C) It converts Java code into C++ D) It skips syntax checking and runs the program Answer: B) It translates the source code into bytecode that can be run on the Java Virtual Machine (JVM) Question 28: If a Java program has syntax errors, what will happen during the compile step? A) The program will run with errors B) The program will compile but not produce a.class file C) The compiler will generate error messages, and the program will not compile D) The program will automatically correct the errors Answer: C) The compiler will generate error messages, and the program will not compile Question 29: In Java, after a program is compiled into bytecode (.class file), where is it executed? A) Directly by the operating system B) On the Central Processing Unit (CPU) C) On the Java Virtual Machine (JVM) D) On the Graphics Processing Unit (GPU) Answer: C) On the Java Virtual Machine (JVM) Variables in Java Question 30: What is a variable in Java? A) A named memory location used to store data B) A method in Java C) A data type in Java D) A fixed value that cannot be changed Answer: A) A named memory location used to store data Question 31: Which of the following is the correct syntax for declaring a variable in Java? A) datatype variableName; B) variableName datatype; C) datatype: variableName; D) var variableName: datatype; Answer: A) datatype variableName; Question 32: Which of these is an example of a valid variable declaration in Java? A) int 1variable; B) String name = "John"; C) float total-amount; D) double!price; Answer: B) String name = "John"; Constants in Java Question 33: How do you define a constant in Java? A) By using the var keyword B) By declaring a variable using the const keyword C) By using the final keyword D) By prefixing the variable with an underscore (_) Answer: C) By using the final keyword Question 34: Which of the following is an example of a constant declaration in Java? A) final int MAX_SIZE = 100; B) const double PI = 3.14159; C) int MAX_SIZE = const 100; D) final const int MAX = 100; Answer: A) final int MAX_SIZE = 100; Built-in Data Types in Java Question 35: Which of the following is NOT a built-in primitive data type in Java? A) int B) char C) float D) String Answer: D) String Question 36: Which of these data types is used to store decimal numbers in Java? A) int B) float C) boolean D) char Answer: B) float Question 37: What is the size (in bits) of the int data type in Java? A) 8 bits B) 16 bits C) 32 bits D) 64 bits Answer: C) 32 bits Question 38: Which of the following is the correct way to declare a character variable in Java? A) char letter = "A"; B) char letter = A; C) char letter = 'A'; D) char letter = A'; Answer: C) char letter = 'A'; Question 39: Which data type in Java can store either true or false? A) byte B) int C) boolean D) short Answer: C) boolean Question 40: What is the range of values that can be stored in a byte data type in Java? A) -128 to 127 B) 0 to 255 C) -32768 to 32767 D) 0 to 65535 Answer: A) -128 to 127 Question 41: What is an identifier in Java? A) A keyword used by the Java language B) A name used to identify a variable, method, class, or other elements C) A reserved word in Java D) A built-in function in Java Answer: B) A name used to identify a variable, method, class, or other elements Question 42: Which of the following is NOT a valid Java identifier? A) _variableName B) $totalAmount C) 1stVariable D) myVariable Answer: C) 1stVariable (Identifiers cannot begin with a digit in Java.) Question 43: Which of the following is true about Java identifier naming conventions? A) Identifiers can only contain uppercase letters B) Identifiers cannot contain numbers C) Identifiers can contain letters, digits, underscores, and dollar signs D) Identifiers can contain special characters such as @ or & Answer: C) Identifiers can contain letters, digits, underscores, and dollar signs Question 44: Which of these is a correct example of a variable name according to Java's naming conventions? A) employeeCount B) Employee_Count C) EMPLOYEECOUNT D) employee$count Answer: A) employeeCount (Camel case is commonly used for variable names in Java.) Question 45: Which of the following is NOT a rule for naming identifiers in Java? A) Identifiers can start with a letter, underscore, or dollar sign B) Identifiers can be of any length C) Identifiers can contain spaces D) Identifiers cannot be a Java keyword Answer: C) Identifiers can contain spaces (Identifiers cannot contain spaces in Java.) Question 46: Which of the following is a valid Java class name, following the naming conventions? A) employeeList B) EmployeeList C) employee_list D) employee$list Answer: B) EmployeeList (Class names in Java usually start with a capital letter and use camel case.) Question 47: Which of these identifiers follows the convention for naming constants in Java? A) MaxValue B) MAX_VALUE C) max_value D) maxValue Answer: B) MAX_VALUE (Constants in Java are typically written in uppercase letters, with words separated by underscores.) Question 48: What is the significance of using the $ symbol in identifiers in Java? A) It is used to mark private variables B) It is commonly used in auto-generated code and for inner classes C) It is reserved for special keywords D) It is used to denote global variables Answer: B) It is commonly used in auto-generated code and for inner classes (Though rarely used in manually written code, $ is allowed in identifiers.) Question 49: Which of the following is a correct identifier for a method following Java naming conventions? A) startProgram B) StartProgram C) start_program D) Start_Program Answer: A) startProgram (Method names should begin with a lowercase letter and follow camel case.) Question 50: Which of these is NOT a valid identifier in Java? A) $Amount B) _result C) float D) averageScore Answer: C) float Question 51: Which of the following is NOT a primitive data type in Java? A) int B) boolean C) String D) float Answer: C) String (String is a class, not a primitive data type.) Question 52: What is the default value of a boolean data type in Java? A) true B) false C) 0 D) null Answer: B) false (By default, a boolean is initialized to false.) Question 53: Which of the following data types is used to store whole numbers in Java? A) float B) char C) int D) boolean Answer: C) int (The int data type is used to store whole numbers.) Question 54: What is the size (in bits) of the double data type in Java? A) 16 bits B) 32 bits C) 64 bits D) 128 bits Answer: C) 64 bits (The double data type is a 64-bit floating-point number.) Question 55: Which of these data types is used to store a single character in Java? A) char B) int C) boolean D) double Answer: A) char (The char data type is used to store a single 16-bit Unicode character.) Question 56: Which of the following is the correct range of values for the byte data type in Java? A) 0 to 255 B) -128 to 127 C) -32768 to 32767 D) -2147483648 to 2147483647 Answer: B) -128 to 127 (The byte data type in Java can hold values from -128 to 127.) Question 57: Which of the following data types in Java has the largest range of values? A) int B) short C) byte D) long Answer: D) long (The long data type can store the largest range of integer values, from -2^63 to 2^63 - 1.) Question 58: Which of these is used to declare a floating-point number in Java? A) int B) boolean C) float D) char Answer: C) float (float is used to declare a single-precision floating-point number.) Question 59: How do you declare a constant in Java? A) Use the const keyword B) Use the static keyword C) Use the final keyword D) Use the constant keyword Answer: C) Use the final keyword (The final keyword is used to declare a constant value in Java.) Question 60: What is the size (in bits) of the int data type in Java? A) 16 bits B) 32 bits C) 64 bits D) 128 bits Answer: B) 32 bits (The int data type is a 32-bit signed integer.) Question 61: Which of the following is NOT a floating-point data type in Java? A) float B) double C) long D) None of the above Answer: C) long (long is an integer data type, not a floating-point type.) Question 62: What is the default value of a char variable in Java? A) 0 B) '\u0000' C) null D) ' ' Answer: B) '\u0000' Question 63: Which of the following is a bitwise operator in Java? A) && B) || C) & D) == Answer: C) & Question 64: Which operator is used to compare two values for equality in Java? A) = B) == C) != D) equals() Answer: B) == Question 65: What is the output of the following Java expression? 10 + 2 * 5 A) 60 B) 20 C) 70 D) 20.0 Answer: B) 20 (Explanation: Multiplication has a higher precedence than addition, so the expression is evaluated as 10 + (2 * 5)) Question 66: Which of the following operators can be used to increment a variable by 1 in Java? A) ++ B) += C) = D) -- Answer: A) ++ Question 67: What does the > 2 A) 32 B) 4 C) 2 D) 1 Answer: B) 2 (Explanation: The right shift operator >> shifts the bits of 8 (1000 in binary) two positions to the right, resulting in 2 (0010 in binary).) Question 72: Which of the following operators can be used to concatenate two strings in Java? A) + B) & C) += D) * Answer: A) + Question 73: Which of the following is not an arithmetic operator in Java? A) + B) - C) * D) && Answer: D) && Question 74: What will be the output of the following expression in Java? 9 / 2 A) 4 B) 4.5 C) 5 D) 5.0 Answer: A) 4 (Explanation: Since both operands are integers, the result will be an integer. The fractional part is discarded.) Question 75: Which operator is used to find the remainder when dividing two numbers in Java? A) / B) % C) // D) * Answer: B) % Question 76: What will be the result of 5 + 3 * 2 in Java? A) 16 B) 13 C) 11 D) 10 Answer: C) 11 (Explanation: According to operator precedence, multiplication is performed first, so the expression becomes 5 + 6 = 11.) Question 77: Which of the following statements correctly increments the value of a variable x by 1? A) x++; B) x += 1; C) x = x + 1; D) All of the above Answer: D) All of the above Question 78: What will be the result of the expression 10 % 4 in Java? A) 2 B) 3 C) 4 D) 6 Answer: A) 2 (Explanation: The modulo operator returns the remainder when 10 is divided by 4, which is 2.) Question 79: Which of the following is the correct way to subtract 5 from 10 in Java? A) 10 + 5 B) 10 - 5 C) 10 * 5 D) 10 / 5 Answer: B) 10 - 5 Question 80: What will be the output of the following expression in Java? 5.0 / 2 A) 2 B) 2.5 C) 2.0 D) 2.25 Answer: B) 2.5 (Explanation: Since one of the operands is a floating-point number, the result will be a floating-point value.) What will be the value of x after the following code is executed? int x = 5; x *= 3; A) 3 B) 5 C) 15 D) 8 Answer: C) 15 (Explanation: x *= 3 is equivalent to x = x * 3, so 5 * 3 = 15.) What is the value of x after x = x - 3 if x was initially 7? A) 10 B) 4 C) 3 D) 0 Answer: B) 4 (Explanation: x = 7 - 3, so x becomes 4.) Which of the following is a valid decision-making statement in Java? A) if B) for C) while D) do Answer: A) if What is the correct syntax for an if statement in Java? A) if (condition) { // statements } B) if condition: // statements C) if condition { // statements } D) if (condition) // statements Answer: A) if (condition) { // statements } Which of the following is true about the else statement in Java? A) It is optional and must follow an if statement. B) It is mandatory after an if statement. C) It can be used independently. D) It must always include a condition. Answer: A) It is optional and must follow an if statement. What will be the output of the following code? java Copy code int x = 5; if (x > 5) { System.out.println("Greater"); } else { System.out.println("Not Greater"); } A) Greater B) Not Greater C) Error D) No output Answer: B) Not Greater (Explanation: Since x is equal to 5, the condition x > 5 is false, so the else block is executed.) Which of the following can replace multiple if-else statements effectively in Java? A) for loop B) switch statement C) while loop D) do-while loop Answer: B) switch statement What is the purpose of a break statement in a switch block? A) To exit the loop. B) To exit the switch block. C) To skip the next case. D) To continue to the next iteration. Answer: B) To exit the switch block. What will be the output of the following code? java Copy code int x = 3; switch(x) { case 1: System.out.println("One"); break; case 2: System.out.println("Two"); break; case 3: System.out.println("Three"); break; default: System.out.println("Default"); } A) One B) Two C) Three D) Default Answer: C) Three (Explanation: The value of x is 3, so the case 3 is executed, and the program prints "Three".) Which of the following operators can be used in an if statement's condition? A) + B) - C) == D) * Answer: C) == (Explanation: The == operator is used to compare two values in a condition.) What will be the result of the following code? java Copy code int num = 4; if (num % 2 == 0) System.out.println("Even"); else System.out.println("Odd"); A) Odd B) Even C) 4 D) Error Answer: B) Even (Explanation: Since num is divisible by 2, it prints "Even".) What is the keyword used to handle multiple alternatives for a single expression in Java's switch statement? A) case B) if C) else D) continue Answer: A) case Which of the following is a repetition control structure in Java? A) if B) for C) switch D) try Answer: B) for What will be the output of the following code? for (int i = 0; i < 5; i++) { System.out.print(i + " "); } A) 0 1 2 3 4 B) 1 2 3 4 5 C) 1 2 3 4 D) 0 1 2 3 4 5 Answer: A) 0 1 2 3 4 Which loop structure is guaranteed to execute at least once, even if the condition is false? A) for B) while C) do-while D) switch Answer: C) do-while (Explanation: The do-while loop checks the condition after executing the loop body, so it runs at least once.) What will be the output of the following code? java Copy code int i = 0; while (i < 3) { System.out.println(i); i++; } A) 1 2 3 B) 0 1 2 C) 0 1 2 3 D) Infinite loop Answer: B) 0 1 2 (Explanation: The loop runs while i is less than 3, printing i and incrementing it after each iteration.) How many times will the following loop execute? java Copy code for (int i = 2; i < 10; i += 2) { System.out.println(i); } A) 4 B) 5 C) 6 D) 8 Answer: B) 5 (Explanation: The loop starts at i = 2 and increments by 2 each time until i is no longer less than 10. It prints 2, 4, 6, 8.) Which of the following can be used to exit a loop early in Java? A) continue B) return C) break D) goto Answer: C) break (Explanation: The break statement is used to exit the loop immediately, regardless of the loop's condition.) What will be the output of the following code? java Copy code int i = 0; do { System.out.println(i); i++; } while (i < 3); A) 1 2 3 B) 0 1 2 C) 0 1 D) 0 1 2 3 Answer: B) 0 1 2 (Explanation: The do-while loop runs at least once and continues while i is less than 3.) What does the continue statement do in a loop? A) Exits the loop. B) Skips the rest of the current iteration and moves to the next iteration. C) Repeats the current iteration. D) Exits the current method. Answer: B) Skips the rest of the current iteration and moves to the next iteration. (Explanation: continue causes the loop to skip the rest of the code for the current iteration and proceed to the next iteration.) Which loop is best suited when the number of iterations is known beforehand? A) for loop B) while loop C) do-while loop D) if-else statement Answer: A) for loop (Explanation: The for loop is ideal when the number of iterations is known because it includes initialization, condition, and increment/decrement all in one line.) What will be the output of the following code? java Copy code for (int i = 0; i < 5; i++) { if (i == 3) { continue; } System.out.println(i); } A) 0 1 2 3 4 B) 0 1 2 4 C) 0 1 2 5 D) 0 2 4 Answer: B) 0 1 2 4 (Explanation: When i == 3, the continue statement is executed, which skips the iteration and does not print 3.)