Java Ch 1-3 Test

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Tcarteronw
T
Tcarteronw
Community Contributor
Quizzes Created: 37 | Total Attempts: 31,024
Questions: 47 | Attempts: 227

SettingsSettingsSettings
Java Ch 1-3 Test - Quiz

Questions and Answers
  • 1. 

    The ____ is the brain of the computer.

    • A.

      Arithmetic logic unit

    • B.

      Central processing unit

    • C.

      Accumulator

    • D.

      Control unit

    Correct Answer
    B. Central processing unit
    Explanation
    The central processing unit (CPU) is often referred to as the brain of the computer because it is responsible for executing instructions and performing calculations. It controls and coordinates all the other components of the computer, including the input and output devices, memory, and storage. The CPU contains the arithmetic logic unit (ALU), which performs mathematical and logical operations, as well as the control unit, which manages the flow of data and instructions. The accumulator is a register within the CPU that stores intermediate results during calculations.

    Rate this question:

  • 2. 

    All data must be brought into ____ before a program can manipulate it.

    • A.

      Secondary storage

    • B.

      Main memory

    • C.

      An output device

    • D.

      All answers are correct

    Correct Answer
    B. Main memory
    Explanation
    Before a program can manipulate data, it must be brought into main memory. Main memory, also known as primary memory or RAM (Random Access Memory), is the primary storage location in a computer where data and instructions are stored temporarily while the program is running. This allows the processor to quickly access and manipulate the data. Secondary storage, such as hard drives or solid-state drives, is used for long-term storage and is not directly accessible by the processor. Output devices are used to display or present the manipulated data, but they do not store it. Therefore, the correct answer is main memory.

    Rate this question:

  • 3. 

    When the power is switched off, everything in ____ is lost.

    • A.

      Main memory

    • B.

      Secondary storage

    • C.

      Hard disks

    • D.

      Floppy disks

    Correct Answer
    A. Main memory
    Explanation
    When the power is switched off, everything in main memory is lost. Main memory, also known as RAM (Random Access Memory), is a volatile type of memory that stores data and instructions that are actively being used by the computer. It is a temporary storage location and requires continuous power supply to retain the data. Once the power is switched off, the data stored in main memory is erased, and the computer starts fresh when powered on again. Therefore, any unsaved work or data in main memory will be lost when the power is turned off.

    Rate this question:

  • 4. 

    The devices that feed data and programs into computers are called ____.

    • A.

      Output devices

    • B.

      Input devices

    • C.

      Monitors

    • D.

      Printers

    Correct Answer
    B. Input devices
    Explanation
    Input devices are the devices that are used to feed data and programs into computers. These devices allow users to interact with the computer and input information or commands. Examples of input devices include keyboards, mice, scanners, and microphones. Output devices, on the other hand, are used to display or present the processed information from the computer, such as monitors or printers. Therefore, the correct answer is "input devices".

    Rate this question:

  • 5. 

    The term MB refers to ____.     

    • A.

      Megabyte

    • B.

      My byte

    • C.

      Moving byte

    • D.

      None of the answers are correct

    Correct Answer
    A. Megabyte
    Explanation
    MB stands for Megabyte, which is a unit of digital information storage equal to 1,048,576 bytes. It is commonly used to measure the size of files, documents, and data storage capacity. Therefore, the correct answer is Megabyte.

    Rate this question:

  • 6. 

    A program called a(n) ____ translates the assembly language instructions into machine language and then executes it.

    • A.

      Loader

    • B.

      Interpreter

    • C.

      Compiler

    • D.

      Assembler

    Correct Answer
    B. Interpreter
    Explanation
    An interpreter is a program that translates the assembly language instructions into machine language and then executes it. Unlike a compiler, which translates the entire program before execution, an interpreter translates and executes the code line by line. This allows for immediate feedback and easier debugging. A loader is responsible for loading the program into memory, while an assembler translates assembly language into machine language. Therefore, the correct answer is interpreter.

    Rate this question:

  • 7. 

    In a Java program, the file name must be the same as the ____.

    • A.

      Method name

    • B.

      Class name

    • C.

      Object name

    • D.

      Data name

    Correct Answer
    B. Class name
    Explanation
    In a Java program, the file name must be the same as the class name. This is because Java follows a naming convention where the file name and the class name should match. This allows the Java compiler to easily locate and load the class when the program is executed. If the file name and the class name do not match, the compiler will throw an error.

    Rate this question:

  • 8. 

    When all ____ errors are removed, the compiler generates the machine code (bytecode in Java).

    • A.

      Logical

    • B.

      Syntax

    • C.

      Linking

    • D.

      Run-time

    Correct Answer
    B. Syntax
    Explanation
    When all syntax errors are removed, the compiler is able to generate the machine code or bytecode in Java. Syntax errors refer to mistakes in the structure or grammar of the code, such as missing semicolons or incorrect variable declarations. Once these errors are fixed, the code becomes syntactically correct and can be compiled into executable machine code.

    Rate this question:

  • 9. 

    Which word is not a reserved word (Keyword)?  

    • A.

      Load

    • B.

      Float

    • C.

      Public

    • D.

      Static

    Correct Answer
    A. Load
    Explanation
    The word "load" is not a reserved word or keyword in programming languages. Reserved words are predefined words that have special meanings and cannot be used as variable names or identifiers. However, "float", "public", and "static" are reserved words in many programming languages, including Java and C++.

    Rate this question:

  • 10. 

    To develop a program to solve a problem, you start by ____.

    • A.

      Analyzing the problem

    • B.

      Implementing the solution in Java

    • C.

      Designing the algorithm

    • D.

      Entering the solution into a computer system

    Correct Answer
    A. Analyzing the problem
    Explanation
    To develop a program to solve a problem, the first step is to analyze the problem. This involves understanding the requirements, constraints, and objectives of the problem. By analyzing the problem, you can identify the inputs, outputs, and the necessary steps to solve it. This initial analysis helps in designing an effective solution and choosing the appropriate programming language and algorithm to implement it.

    Rate this question:

  • 11. 

    In Java, the mechanism that allows you to combine data and operations on the data into a single unit is called a(n) ____.

    • A.

      Object

    • B.

      Class

    • C.

      Method

    Correct Answer
    C. Method
    Explanation
    In Java, a method is a mechanism that allows you to combine data and operations on the data into a single unit. Methods are used to perform specific actions on objects and can be called to execute a block of code. They provide a way to organize and encapsulate functionality within a class, making code more modular and reusable. Therefore, the correct answer for this question is method.

    Rate this question:

  • 12. 

    The ____ rules of a programming language tell you which statements are legal, or accepted by the programming language.

    • A.

      Semantic

    • B.

      Logical

    • C.

      Syntax

    • D.

      Grammatical

    Correct Answer
    C. Syntax
    Explanation
    The syntax rules of a programming language define the structure and format of valid statements in the language. These rules determine whether a statement is written correctly according to the language's grammar and punctuation. Syntax errors occur when a statement violates these rules and are usually detected by the compiler or interpreter. Therefore, the correct answer is "syntax."

    Rate this question:

  • 13. 

    Which of the following is NOT a special symbol in Java?

    • A.

      +

    • B.

      #

    • C.

      !=

    • D.

      ?

    Correct Answer
    B. #
    Explanation
    In Java, the special symbols include +, !=, and ?. However, # is not considered a special symbol in Java. It is commonly used in other programming languages like C or C++ to indicate preprocessor directives, but in Java, it does not have any special meaning or usage.

    Rate this question:

  • 14. 

    Which of the following identifiers is not valid?

    • A.

      Sum

    • B.

      _sum

    • C.

      $sum

    • D.

      5sum

    Correct Answer
    D. 5sum
    Explanation
    The identifier "5sum" is not valid because identifiers in programming languages cannot start with a number. They must start with a letter or an underscore.

    Rate this question:

  • 15. 

    Oranges = oranges * (expression); is equivalent to oranges *= expression;

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    The given statement is true because the shorthand operator "*=" is used to multiply the value of the variable "oranges" by the value of "expression" and assign the result back to "oranges". This is equivalent to the first statement "oranges = oranges * (expression)". Therefore, both statements have the same effect of updating the value of "oranges" by multiplying it with "expression".

    Rate this question:

  • 16. 

    Primitive Data types in Java do not include:

    • A.

      Integral

    • B.

      Floating Point

    • C.

      Boolean

    • D.

      Decimal

    Correct Answer
    D. Decimal
    Explanation
    The primitive data types in Java include integral (byte, short, int, long), floating point (float, double), and boolean. However, decimal is not a primitive data type in Java. Decimal values are typically represented using the non-primitive data type BigDecimal, which provides more precise decimal calculations.

    Rate this question:

  • 17. 

    Integral Data Types in Java do not include:

    • A.

      Char

    • B.

      Long

    • C.

      Int

    • D.

      Double

    Correct Answer
    D. Double
    Explanation
    Integral data types in Java are used to store whole numbers without any fractional parts. They include char, long, and int. However, double is not an integral data type in Java. It is a floating-point data type that can store numbers with decimal places. Therefore, the correct answer is double.

    Rate this question:

  • 18. 

    A null or empty string has at least one character that is a space.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    This statement is false because a null or empty string does not have any characters, including spaces. A null string refers to a variable that has not been assigned a value, while an empty string refers to a string with no characters. In both cases, there are no spaces or any other characters present.

    Rate this question:

  • 19. 

    Named constants

    • A.

      They cannot be changed during program execution

    • B.

      They are declared using the reserved word final

    • C.

      They are initialized when they are declared

    • D.

      They should be created in all capital letters

    Correct Answer(s)
    A. They cannot be changed during program execution
    B. They are declared using the reserved word final
    C. They are initialized when they are declared
    D. They should be created in all capital letters
    Explanation
    Named constants are values that cannot be changed during program execution. They are declared using the reserved word final, meaning their value cannot be modified once assigned. Named constants are also initialized when they are declared, meaning they are given an initial value when they are defined. Additionally, it is a convention to create named constants in all capital letters to easily distinguish them from variables.

    Rate this question:

  • 20. 

    Int num1; The statement above declares and initializes the value of num1.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    Initializing the value means you make the variable equal to something.
    i.e.
    int num1 = 15;

    Rate this question:

  • 21. 

    Which of the following methods would you use to read in a number such as 2.3 for a variable?

    • A.

      Next();

    • B.

      NextInt();

    • C.

      NextDouble();

    • D.

      NextLine();

    Correct Answer
    C. NextDouble();
    Explanation
    To read in a number such as 2.3 for a variable, the most appropriate method to use would be nextDouble(). This method is specifically designed to read in double values from the input. next() would only read in the next token as a string, nextInt() would only read in the next token as an integer, and nextLine() would read in the entire line as a string. Therefore, nextDouble() is the correct method to use in this scenario.

    Rate this question:

  • 22. 

    The value of the expression 26 – 14 % 3 + 1 is ____.

    • A.

      0

    • B.

      1

    • C.

      24

    • D.

      25

    Correct Answer
    D. 25
    Explanation
    The expression is evaluated using the order of operations. First, the modulo operator (%) is applied, giving a result of 2. Then, the subtraction and addition operators are applied, giving a final result of 25.

    Rate this question:

  • 23. 

    Which of the following is a legal identifier?

    • A.

      Program!

    • B.

      Program_1

    • C.

      1program

    • D.

      Program 1

    Correct Answer
    B. Program_1
    Explanation
    A legal identifier is a name that can be used to identify a variable, function, or any other programming entity. It must follow certain rules, such as starting with a letter or underscore, and can only contain letters, numbers, or underscores. In this case, "program_1" is a legal identifier because it starts with a letter, followed by letters and an underscore, which follows the rules for identifiers in programming languages.

    Rate this question:

  • 24. 

    Which of the following is a valid int value?

    • A.

      46,259

    • B.

      46259

    • C.

      462.59

    • D.

      46,259.59

    Correct Answer
    B. 46259
    Explanation
    A valid int value is a whole number without any decimal points. Therefore, the only option that fits this criteria is 46259. The other options either have decimal points or commas, which make them invalid int values.

    Rate this question:

  • 25. 

    Consider the following sequence of statements.   String str; int num1, num2; num1 = 13; num2 = 24; str = "The sum = " + num1 + num2; What is the final value stored in str?

    • A.

      The sum = 37

    • B.

      The sum = 13 24

    • C.

      The sum = 13 + 24

    • D.

      The sum = 1324

    Correct Answer
    D. The sum = 1324
    Explanation
    The final value stored in str is "The sum = 1324" because the concatenation operator (+) is used to combine the string "The sum = " with the values of num1 and num2. Since num1 and num2 are both integers, they are converted to strings before being concatenated. Therefore, the final result is the string "The sum = " followed by the string representations of num1 and num2, which are "13" and "24" respectively.

    Rate this question:

  • 26. 

    What is the output of the following statement? System.out.println("Welcome \n Home");

    • A.

      WelcomeHome

    • B.

      Welcome Home

    • C.

      Welcome Home

    • D.

      Welcome \n Home

    Correct Answer
    C. Welcome Home
    Explanation
    The output of the given statement is "Welcome Home". This is because the statement is using the println() method to print the string "Welcome Home" to the console. The println() method adds a new line after printing the string, so the output will be displayed on separate lines.

    Rate this question:

  • 27. 

    Which of the following is the correct syntax for commenting in Java?

    • A.

      # Enter Comments Here

    • B.

      'Enter Comments Here

    • C.

      /* Enter Comments Here*/

    • D.

      ** Enter Comments Here **

    Correct Answer
    C. /* Enter Comments Here*/
    Explanation
    The correct syntax for commenting in Java is /* Enter Comments Here */. This syntax allows you to comment out multiple lines of code or add a comment on a single line. The other options (#, ', **) are not valid syntax for commenting in Java.

    Rate this question:

  • 28. 

    Suppose x = 2 and y = 3.  If the statement x *= y; is executed once, what is the value of x?

    • A.

      2

    • B.

      3

    • C.

      5

    • D.

      6

    Correct Answer
    D. 6
    Explanation
    The statement "x *= y" is a compound assignment operator that multiplies the value of x by the value of y and then assigns the result back to x. In this case, since x is initially 2 and y is 3, the value of x after executing the statement will be 6.

    Rate this question:

  • 29. 

    Suppose that you have the following figure. According to this figure,  str contains ____.

    • A.

      The value 2500

    • B.

      The value Java Programming.

    • C.

      The address 2500

    Correct Answer
    C. The address 2500
    Explanation
    Based on the given figure, it can be inferred that "str" contains the address 2500. This is because the figure represents a memory diagram, and the arrow pointing to "str" indicates that it is storing the memory address 2500.

    Rate this question:

  • 30. 

    What is the value of the following statement?   Math.pow(2,4)

    • A.

      6.0

    • B.

      8.0

    • C.

      16.0

    • D.

      24.0

    Correct Answer
    C. 16.0
    Explanation
    The Math.pow() function in JavaScript is used to raise a number to a certain power. In this case, Math.pow(2,4) means 2 raised to the power of 4. So, the value of this statement is 16.0, as 2^4 equals 16.

    Rate this question:

  • 31. 

    An expression such as str.length(); is an example of a(n) ____.

    • A.

      System call

    • B.

      String object

    • C.

      Class

    • D.

      Method call

    Correct Answer
    D. Method call
    Explanation
    The expression "str.length();" is an example of a method call. In this case, the method "length()" is being called on the string object "str" to retrieve the length of the string.

    Rate this question:

  • 32. 

    Which of the following outputs 56734.987 to three decimal places?

    • A.

      System.out.printf("3f%", 56734.9875);

    • B.

      System.out.printf(".3f%", 56734.9875);

    • C.

      System.out.printf(".03f%", 56734.9875);

    Correct Answer
    B. System.out.printf(".3f%", 56734.9875);
    Explanation
    The correct answer is System.out.printf(".3f%", 56734.9875);. This is because the ".3f" format specifier in the printf statement specifies that the output should be formatted as a floating-point number with three decimal places.

    Rate this question:

  • 33. 

    Which of the following statements would produce the figure above?

    • A.

      Name = JOptionPane.showInputDialog("Enter your name and press OK");

    • B.

      Name = JOptionPane.showMessageDialog("Enter your name and press OK", JOptionPane.QUESTION_MESSAGE, null);

    • C.

      Name = System.in();

    • D.

      System.out.println("Enter your name and press OK");

    Correct Answer
    A. Name = JOptionPane.showInputDialog("Enter your name and press OK");
    Explanation
    The correct answer is "name = JOptionPane.showInputDialog("Enter your name and press OK");". This statement would produce a dialog box with a text field where the user can enter their name and press OK.

    Rate this question:

  • 34. 

      What would be output by the following statement: Assume that int num1 = 12; int num2 = 26; str = "The sum = " + (num1 + num2);  

    • A.

      "The sum = 38";

    • B.

      "The sum = (12 + 26)";

    • C.

      "The sum = " + (num1 + num2);

    Correct Answer
    A. "The sum = 38";
    Explanation
    The output of the given statement would be "The sum = 38". This is because the statement assigns the string "The sum = " concatenated with the sum of num1 and num2 (which is 38) to the variable str.

    Rate this question:

  • 35. 

    Which escape sequence is used for a tab space?

    • A.

      \n

    • B.

      \tb

    • C.

      \t

    • D.

      \b

    Correct Answer
    C. \t
    Explanation
    The escape sequence \t is used for a tab space.

    Rate this question:

  • 36. 

    Which package needs to be imported to read and write to outside files?

    • A.

      Java.io

    • B.

      Java.util

    • C.

      Javax.swing

    • D.

      Java.awt

    Correct Answer
    A. Java.io
    Explanation
    The correct answer is java.io. The java.io package in Java provides classes for input and output operations, including reading and writing to external files. This package contains classes such as FileReader, FileWriter, BufferedReader, BufferedWriter, and many others that are essential for file handling in Java. Therefore, to read and write to outside files, the java.io package needs to be imported.

    Rate this question:

  • 37. 

    This type of variable stores the address of the object containing the data

    • A.

      Primitive

    • B.

      Reference

    • C.

      Instance

    Correct Answer
    B. Reference
    Explanation
    A reference variable in programming is used to store the address of an object that contains data. Unlike primitive variables that store the actual value, a reference variable holds a reference or pointer to the memory location where the object is stored. This allows us to access and manipulate the data within the object through the reference variable. Therefore, the given correct answer is "reference".

    Rate this question:

  • 38. 

      An object is an instance of a class and the operator new is used to instantiate an object .

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    In object-oriented programming, an object is created by instantiating a class using the "new" operator. This means that the "new" operator is used to allocate memory for the object and initialize its attributes and methods based on the class definition. Therefore, the given statement is true.

    Rate this question:

  • 39. 

    To use a predefined method you must know

    • A.

      Name of the class containing method (i.e. Math)

    • B.

      Name of the package containing class (i.e. java.lang)

    • C.

      Name of the method & the parameters

    • D.

      Name of the object created

    Correct Answer(s)
    A. Name of the class containing method (i.e. Math)
    B. Name of the package containing class (i.e. java.lang)
    C. Name of the method & the parameters
    Explanation
    To use a predefined method, you must know the name of the class containing the method (e.g. Math), the name of the package containing the class (e.g. java.lang), and the name of the method along with its parameters. These pieces of information are necessary to correctly call and utilize the predefined method in your code.

    Rate this question:

  • 40. 

      Dot (.) Operator: used to access the method in the class

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    The dot (.) operator is used to access the method in a class. This means that by using the dot operator, we can call or invoke a method that is defined within a class. Therefore, the statement "Dot (.) Operator: used to access the method in the class" is true.

    Rate this question:

  • 41. 

      String variables are reference variables

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    In Java, string variables are reference variables because they store the memory address of the actual string object rather than the value itself. This means that when we assign a string to a variable, the variable holds a reference to the memory location where the string is stored. This allows us to manipulate and access the string using the variable.

    Rate this question:

  • 42. 

      A String object is an instance of class String

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    The statement is true because in Java, a String object is indeed an instance of the class String. In object-oriented programming, a class is a blueprint for creating objects, and an instance is a specific object created from that blueprint. Therefore, when we create a String object in Java, we are creating an instance of the String class.

    Rate this question:

  • 43. 

    To convert a string consisting of an integer to a value of the type int, we use the following expression:

    • A.

      Integer.parseInt(strExpression)

    • B.

      Double.parseDouble(strExpression)

    • C.

      String.parseString(strExpression)

    • D.

      Float.parseFloat(strExpression)

    Correct Answer
    A. Integer.parseInt(strExpression)
    Explanation
    The correct answer is Integer.parseInt(strExpression) because this expression is used to convert a string consisting of an integer to an int value. The parseInt() method parses the string argument as a signed decimal integer and returns the corresponding int value.

    Rate this question:

  • 44. 

      Integer, Float, and Double are classes designed to convert a numeric string into a number These classes are called _________________ classes

    • A.

      Wrapper

    • B.

      String

    • C.

      Numeric

    • D.

      Flexible

    Correct Answer
    A. Wrapper
    Explanation
    The Integer, Float, and Double classes are known as wrapper classes. These classes provide methods and functionality to convert a numeric string into the respective number type (integer, float, or double). Wrapper classes are used to wrap primitive data types and provide additional methods and functionality that are not available in the primitive types.

    Rate this question:

  • 45. 

    What is the value of the following? double x = 15.674; double y = 235.73;  String.format("%.2f ",x)

    • A.

      15.67

    • B.

      15.68

    • C.

      15.674

    • D.

      235.73

    Correct Answer
    A. 15.67
    Explanation
    The correct answer is 15.67. The code uses the String.format() method to format the value of variable x as a string with 2 decimal places. Since x is 15.674, it will be rounded down to 15.67.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 20, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Sep 28, 2011
    Quiz Created by
    Tcarteronw
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.