1. Home
  2. Explore
  3. Computer Science
  4. Java Programming Fundamentals

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Java Programming Fundamentals

  • 101Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Tools and Execution

What is the primary purpose of the Java Development Kit (JDK)?

101 questions

  1. 1

    What is the primary purpose of the Java Development Kit (JDK)?

    1. A

      To provide only a graphical interface for running Java applications

    2. B

      To organize Java classes into packages automatically

    3. C

      To provide tools and software needed to develop Java applications

    4. D

      To convert Java source code directly into processor-specific code without a runtime

    Correct answer
    C

    To provide tools and software needed to develop Java applications

    Explanation

    The JDK is a software development environment that includes the Java compiler, reusable code, a runtime environment, and other development tools.

  2. 2

    Which statement correctly describes the Java compiler’s role when a Java program is built?

    1. A

      It converts Java source code into Java bytecode

    2. B

      It converts Java bytecode into source code for the operating system

    3. C

      It runs the program’s main method while checking syntax

    4. D

      It converts Java source code directly into operating-system native code

    Correct answer
    A

    It converts Java source code into Java bytecode

    Explanation

    The Java compiler compiles source files into bytecode, which is stored in class files.

  3. 3

    What does the Java Virtual Machine (JVM) do when a Java program runs?

    1. A

      It converts Java source files into bytecode during editing

    2. B

      It translates native operating-system code into Java source code

    3. C

      It organizes compiled classes into packages

    4. D

      It translates Java bytecode into native code for the underlying operating system

    Correct answer
    D

    It translates Java bytecode into native code for the underlying operating system

    Explanation

    The JVM loads bytecode and translates it into native code that the underlying operating system can execute.

  4. 4

    Why can the same Java bytecode run on different operating systems?

    1. A

      The operating system ignores the bytecode and runs the original source file

    2. B

      Java source code is rewritten by the compiler for every operating system automatically

    3. C

      Java bytecode contains separate native programs for every operating system

    4. D

      Each operating system with a Java runtime can use its JVM to translate the bytecode

    Correct answer
    D

    Each operating system with a Java runtime can use its JVM to translate the bytecode

    Explanation

    Bytecode is platform independent; a Java runtime environment on each platform provides a JVM to translate it for that operating system.

  5. 5

    Which pairing correctly describes the relationship between a Java Runtime Environment (JRE) and a JVM?

    1. A

      The JRE includes a JVM that runs Java bytecode

    2. B

      The JRE is a source-code editor, and the JVM is its compiler

    3. C

      The JRE stores source files, and the JVM organizes them into packages

    4. D

      The JVM includes a JRE that compiles Java source code

    Correct answer
    A

    The JRE includes a JVM that runs Java bytecode

    Explanation

    The JRE includes the JVM, which is the component that runs Java bytecode.

  6. 6

    What is a command-line application, as contrasted with a graphical user-interface application?

    1. A

      It must be written in a language other than Java

    2. B

      It can be run from a command line and does not provide a graphical user interface

    3. C

      It cannot accept input from a user

    4. D

      It can only be run inside a code editor and must provide a graphical interface

    Correct answer
    B

    It can be run from a command line and does not provide a graphical user interface

    Explanation

    A command-line application runs from a command line and does not have a graphical user interface.

  7. 7

    What is the purpose of a Java package in a project?

    1. A

      To specify which code editor the project uses

    2. B

      To translate bytecode into native code

    3. C

      To group related classes and provide a namespace for them

    4. D

      To store the values of primitive variables while a program runs

    Correct answer
    C

    To group related classes and provide a namespace for them

    Explanation

    Packages group related classes and provide namespaces, which helps organize projects that may contain many classes.

  8. 8

    Why might a project use a reversed company domain as its base package name?

    1. A

      It tells the compiler which operating system to target

    2. B

      It names the JVM that will execute the program

    3. C

      It creates a distinctive namespace for the project’s classes

    4. D

      It determines the data types available in the project

    Correct answer
    C

    It creates a distinctive namespace for the project’s classes

    Explanation

    Using a reversed domain is a convention for creating a namespace that helps distinguish a project’s classes; an actually registered domain is not required.

  9. 9

    Which statement about a Java source file is correct?

    1. A

      A Java source file uses the .class extension

    2. B

      A Java source file contains only bytecode, not source code

    3. C

      A Java source file uses the .java extension

    4. D

      A Java source file must have the same name as its package

    Correct answer
    C

    A Java source file uses the .java extension

    Explanation

    Java source files use the .java extension; compiled bytecode is commonly stored in .class files.

  10. 10

    Which statement correctly distinguishes Java SE from Java EE in the described Java editions?

    1. A

      Java SE is the core platform, while Java EE adds libraries for large-scale and distributed systems

    2. B

      Java SE is built on Java EE and provides extra libraries for smart cards

    3. C

      Java SE is only a code editor, while Java EE is the Java compiler

    4. D

      Java SE is a subset for smart cards, while Java EE is designed for mobile devices

    Correct answer
    A

    Java SE is the core platform, while Java EE adds libraries for large-scale and distributed systems

    Explanation

    Java Standard Edition is the core Java platform; Java Enterprise Edition builds on it and adds libraries for large-scale, distributed, multi-tier software.

  11. 11

    In Java, what is a function that belongs to a class called?

    1. A

      A field

    2. B

      A package

    3. C

      A method

    4. D

      A parameter

    Correct answer
    C

    A method

    Explanation

    A function that is part of a class is referred to as a method.

  12. 12

    What is the role of a class in a Java program?

    1. A

      It acts as a container for related methods and other members

    2. B

      It translates source code into bytecode

    3. C

      It stores only one primitive value

    4. D

      It acts as a value passed into a method

    Correct answer
    A

    It acts as a container for related methods and other members

    Explanation

    A class organizes related code by serving as a container for methods and other members.

  13. 13

    What is the role of the main method in a Java program?

    1. A

      It is required to return a string to the operating system

    2. B

      It is the package that contains every Java class

    3. C

      It is the method that compiles all source files

    4. D

      It is the program’s entry point when the program is run

    Correct answer
    D

    It is the program’s entry point when the program is run

    Explanation

    When a Java program is executed, its main method is called as the entry point.

  14. 14

    What does the void return type indicate for a method?

    1. A

      The method can return any primitive value

    2. B

      The method does not return a value

    3. C

      The method is not allowed to contain statements

    4. D

      The method is available only inside its own class

    Correct answer
    B

    The method does not return a value

    Explanation

    void specifies that a method does not return a value.

  15. 15

    What is the purpose of a method parameter?

    1. A

      It marks the end of a Java statement

    2. B

      It determines which package contains the method

    3. C

      It provides a named input through which a value can be passed to a method

    4. D

      It specifies the value returned by the method

    Correct answer
    C

    It provides a named input through which a value can be passed to a method

    Explanation

    Parameters are the named inputs declared in a method; callers supply values, called arguments, for those inputs.

  16. 16

    What is the purpose of an access modifier such as public?

    1. A

      It controls whether other classes or methods can access a declaration

    2. B

      It specifies whether a method returns a value

    3. C

      It determines whether a class is compiled into bytecode

    4. D

      It indicates that a variable is a constant

    Correct answer
    A

    It controls whether other classes or methods can access a declaration

    Explanation

    Access modifiers, including public and private, control the accessibility of classes and members.

  17. 17

    Which naming convention is used for Java class names in the described conventions?

    1. A

      Camel case, with only the first word beginning with an uppercase letter

    2. B

      All lowercase letters with words separated by underscores

    3. C

      All uppercase letters with words separated by spaces

    4. D

      Pascal case, with each word beginning with an uppercase letter

    Correct answer
    D

    Pascal case, with each word beginning with an uppercase letter

    Explanation

    Class names use Pascal naming convention, in which the first letter of every word is uppercase.

  18. 18

    Which naming convention is used for Java method names in the described conventions?

    1. A

      Camel case, with the first word lowercase and later words capitalized

    2. B

      All uppercase letters with words separated by underscores

    3. C

      All lowercase letters with no distinction between words

    4. D

      Pascal case, with every word beginning with an uppercase letter

    Correct answer
    A

    Camel case, with the first word lowercase and later words capitalized

    Explanation

    Method names use camel case: the first word begins with a lowercase letter and each later word begins with an uppercase letter.

  19. 19

    What does a semicolon normally indicate in Java?

    1. A

      The end of a package name

    2. B

      The beginning of a class

    3. C

      The start of a comment

    4. D

      The end of a statement

    Correct answer
    D

    The end of a statement

    Explanation

    Java statements are terminated with semicolons.

  20. 20

    What is the effect of prefixing Java code with //?

    1. A

      It declares a method that returns no value

    2. B

      It makes the following text a string literal

    3. C

      It imports a class from another package

    4. D

      It marks the following text on that line as a comment, not executable code

    Correct answer
    D

    It marks the following text on that line as a comment, not executable code

    Explanation

    A double slash begins a single-line comment; comments explain code and are not executed.

  21. 21

    Which statement correctly compares primitive and reference types in Java?

    1. A

      Primitive variables hold object addresses, while reference variables hold only simple values

    2. B

      Both categories must be initialized using the new operator

    3. C

      Both categories always store their values directly in the variable

    4. D

      Primitive variables hold simple values, while reference variables hold references to objects

    Correct answer
    D

    Primitive variables hold simple values, while reference variables hold references to objects

    Explanation

    Primitive types are used for simple values; reference types refer to more complex objects.

  22. 22

    Which primitive type is designed to store a single character?

    1. A

      double

    2. B

      String

    3. C

      boolean

    4. D

      char

    Correct answer
    D

    char

    Explanation

    The primitive type char stores a single character; String is a reference type for sequences of characters.

  23. 23

    Which primitive type stores either true or false?

    1. A

      char

    2. B

      boolean

    3. C

      String

    4. D

      int

    Correct answer
    B

    boolean

    Explanation

    A boolean stores one of two values: true or false.

  24. 24

    Which statement correctly describes float and double in the lesson’s comparison of primitive types?

    1. A

      Both store numbers with decimal points, and double uses more memory than float

    2. B

      float is a reference type, while double is a primitive type

    3. C

      Both store whole numbers, and float uses more memory than double

    4. D

      float stores characters, while double stores boolean values

    Correct answer
    A

    Both store numbers with decimal points, and double uses more memory than float

    Explanation

    float and double represent floating-point numbers; float takes four bytes and double takes eight bytes.

  25. 25

    Why might a program use a byte rather than an int to store a person’s age?

    1. A

      A byte is a reference type that stores objects more efficiently

    2. B

      An int cannot store whole numbers

    3. C

      A byte can store decimal values, while an int cannot

    4. D

      A byte can represent the small range needed for an age while using less memory

    Correct answer
    D

    A byte can represent the small range needed for an age while using less memory

    Explanation

    The described byte range is sufficient for ordinary ages, and it uses less memory than an int.

  26. 26

    Why does a large integer literal assigned to a long sometimes need an L suffix?

    1. A

      The suffix converts a string containing digits into a long

    2. B

      Integer literals are treated as int by default, so the suffix marks the literal as a long

    3. C

      The suffix allocates additional memory for the variable at runtime

    4. D

      The suffix tells Java to treat the literal as a float

    Correct answer
    B

    Integer literals are treated as int by default, so the suffix marks the literal as a long

    Explanation

    Java treats integer literals as int by default; an L suffix marks a literal as a long when it exceeds the int range.

  27. 27

    Why does a decimal literal assigned to a float need an F suffix in the described example?

    1. A

      Decimal literals are treated as strings by default, and F converts them to numbers

    2. B

      The suffix marks the variable as a constant

    3. C

      Decimal literals are treated as double by default, and F marks the literal as a float

    4. D

      The suffix tells Java to round the value to a whole number

    Correct answer
    C

    Decimal literals are treated as double by default, and F marks the literal as a float

    Explanation

    A decimal literal is treated as a double by default, so an F suffix is used to represent it as a float.

  28. 28

    What happens when one primitive variable is assigned to another primitive variable of the same type?

    1. A

      The value is copied, so later changes to one variable do not change the other

    2. B

      The variables are merged into a single memory location

    3. C

      The assignment is rejected because primitive values cannot be copied

    4. D

      Both variables become references to one shared object

    Correct answer
    A

    The value is copied, so later changes to one variable do not change the other

    Explanation

    Primitive values are copied by value; the two variables remain independent.

  29. 29

    What happens when a reference variable is assigned to another reference variable?

    1. A

      The reference variable takes ownership of the class definition

    2. B

      The reference is copied, so both variables can refer to the same object

    3. C

      The assignment converts the object into a primitive value

    4. D

      The object is automatically duplicated into a separate object

    Correct answer
    B

    The reference is copied, so both variables can refer to the same object

    Explanation

    Reference types are copied by reference. The variables can refer to the same object, so changes through one reference can be observed through the other.

  30. 30

    What does the new operator do when creating an ordinary object such as a Date?

    1. A

      It releases memory used by an existing object

    2. B

      It imports the class from its package

    3. C

      It allocates memory for an object and creates an instance of its class

    4. D

      It converts the class into a primitive type

    Correct answer
    C

    It allocates memory for an object and creates an instance of its class

    Explanation

    The new operator is used to create an object instance and allocate memory for it; the runtime manages object memory afterward.

  31. 31

    What is an object in relation to a class?

    1. A

      A method parameter with no assigned value

    2. B

      An instance created from the class’s definition or blueprint

    3. C

      A primitive value used to define the class

    4. D

      A package that contains the class’s methods

    Correct answer
    B

    An instance created from the class’s definition or blueprint

    Explanation

    A class defines a template or blueprint; an object is an instance of that class.

  32. 32

    Why can code use a class from another package only after importing it, unless it is automatically available?

    1. A

      The import converts the class into a primitive type

    2. B

      The import changes the class’s access modifier to public

    3. C

      The import compiles the class into the main method

    4. D

      The import makes the class from that package available by its simple name

    Correct answer
    D

    The import makes the class from that package available by its simple name

    Explanation

    An import allows code to refer to a class from another package without writing its full package name; java.lang is automatically available.

  33. 33

    Why can a Java string be initialized from a string literal without using new?

    1. A

      The new operator cannot create string objects

    2. B

      The literal is stored as a char variable instead

    3. C

      It is a convenient shorthand, even though String remains a reference type

    4. D

      String literals make String a primitive type

    Correct answer
    C

    It is a convenient shorthand, even though String remains a reference type

    Explanation

    Java provides a shorter way to create strings using literals, but strings are still reference types.

  34. 34

    What does concatenating two strings with + do?

    1. A

      It changes the original strings in place

    2. B

      It converts both strings into character arrays

    3. C

      It joins their text into a new string

    4. D

      It compares the strings by their memory addresses

    Correct answer
    C

    It joins their text into a new string

    Explanation

    The + operator concatenates, or joins, strings to produce combined text.

  35. 35

    What does the length() method return for a string?

    1. A

      The memory address of the string

    2. B

      The number of characters in the string

    3. C

      The index of the first character

    4. D

      The number of words in the string

    Correct answer
    B

    The number of characters in the string

    Explanation

    length() returns the number of characters in a string.

  36. 36

    What does indexOf return when the requested character or text does not occur in a string?

    1. A

      0

    2. B

      The string’s length

    3. C

      A boolean value of false

    4. D

      -1

    Correct answer
    D

    -1

    Explanation

    The described indexOf behavior is to return -1 when the searched-for content is not found.

  37. 37

    What does the replace method do to the original string?

    1. A

      It returns a new string with the requested replacements; the original remains unchanged

    2. B

      It returns the index of the first character that was replaced

    3. C

      It deletes the original string and stores the replacement in its place

    4. D

      It modifies the original string directly and returns no value

    Correct answer
    A

    It returns a new string with the requested replacements; the original remains unchanged

    Explanation

    Strings are immutable in Java, so replace returns a new string instead of modifying the original.

  38. 38

    What does calling toLowerCase() on a string produce?

    1. A

      A boolean indicating whether the string was lowercase

    2. B

      A changed version of the original string stored in the same object

    3. C

      A new string with its characters converted to lowercase

    4. D

      The number of lowercase characters in the string

    Correct answer
    C

    A new string with its characters converted to lowercase

    Explanation

    toLowerCase() returns a new string; it does not mutate the original string.

  39. 39

    What is the purpose of the trim() method?

    1. A

      To remove every space between words in a string

    2. B

      To shorten a string to its first character

    3. C

      To remove leading and trailing whitespace from a string

    4. D

      To convert all characters to uppercase

    Correct answer
    C

    To remove leading and trailing whitespace from a string

    Explanation

    trim() removes extra whitespace at the beginning and end of a string.

  40. 40

    How can two string values be compared for equal text using the methods described?

    1. A

      Use = between the strings

    2. B

      Call equals on one string and pass the other string

    3. C

      Call length on both strings and compare the results

    4. D

      Use == to guarantee their text is compared

    Correct answer
    B

    Call equals on one string and pass the other string

    Explanation

    The equals method compares string values. The equality operator on references compares references, not necessarily their text.

  41. 41

    Why may next() fail to read a user’s full name containing a space?

    1. A

      It reads a single token, so text after the space remains unread

    2. B

      It reads only numeric input

    3. C

      It converts the input to lowercase automatically

    4. D

      It removes all characters after the first letter

    Correct answer
    A

    It reads a single token, so text after the space remains unread

    Explanation

    next() reads one token; whitespace separates tokens, so a name with spaces requires more than one token-reading call or a line-reading method.

  42. 42

    Which escape sequence inserts a new line within a Java string?

    1. A

      \n

    2. B

      `\

    3. C

      \\

    4. D

      \t

    Correct answer
    A

    \n

    Explanation

    The escape sequence \n inserts a new line. \t inserts a tab, \\ represents a backslash, and \" represents a double quote inside a string.

  43. 43

    Why must a backslash in a string be escaped when representing a Windows path?

    1. A

      A backslash converts the string to a file object

    2. B

      A backslash marks the end of a string, so it must be doubled to close the string

    3. C

      A backslash begins escape sequences, so a literal backslash is written as \\

    4. D

      A backslash is treated as a comment marker inside strings

    Correct answer
    C

    A backslash begins escape sequences, so a literal backslash is written as \\

    Explanation

    Backslash introduces escape sequences in Java strings; \\ in source represents one literal backslash in the resulting string.

  44. 44

    How is an array’s first element accessed in Java?

    1. A

      Using index 0

    2. B

      Using index equal to the array’s length

    3. C

      Using index 1

    4. D

      Using the array’s first field

    Correct answer
    A

    Using index 0

    Explanation

    Java arrays are zero-indexed, so the first element is at index 0.

  45. 45

    What happens when a program accesses an array index outside its valid range?

    1. A

      An exception is raised and the program may terminate

    2. B

      The value is silently stored in the next available position

    3. C

      Java automatically expands the array to include the index

    4. D

      The invalid index is treated as the last valid index

    Correct answer
    A

    An exception is raised and the program may terminate

    Explanation

    Accessing an index outside an array’s bounds raises an exception, which can cause the program to crash if not handled.

  46. 46

    How can a program obtain a readable string representation of a one-dimensional primitive array?

    1. A

      Use Arrays.deepToString only

    2. B

      Call length on the array and print that value

    3. C

      Print the array variable directly

    4. D

      Use the appropriate overloaded Arrays.toString method

    Correct answer
    D

    Use the appropriate overloaded Arrays.toString method

    Explanation

    The overloaded Arrays.toString methods produce a readable representation of one-dimensional arrays.

  47. 47

    What is the default value of an unassigned element in an integer array?

    1. A

      0

    2. B

      An empty string

    3. C

      null

    4. D

      An unspecified random value

    Correct answer
    A

    0

    Explanation

    Integer array elements are initialized to 0 by default; boolean elements default to false.

  48. 48

    What is an important limitation of Java arrays after they are created?

    1. A

      Their elements cannot be read by index

    2. B

      They can contain only primitive values

    3. C

      Their length is fixed

    4. D

      They cannot be sorted

    Correct answer
    C

    Their length is fixed

    Explanation

    A Java array has a fixed length after creation. Collections are used when a resizable structure is needed.

  49. 49

    Which method sorts an array using the Arrays class?

    1. A

      array.length()

    2. B

      Arrays.order(array)

    3. C

      Arrays.toString(array)

    4. D

      Arrays.sort(array)

    Correct answer
    D

    Arrays.sort(array)

    Explanation

    The sort method of the Arrays class sorts an array.

  50. 50

    How does a two-dimensional array differ from a one-dimensional array?

    1. A

      It has two indices, such as a row index and a column index

    2. B

      It must contain exactly two arrays

    3. C

      It stores only two values total

    4. D

      It can contain only numeric data

    Correct answer
    A

    It has two indices, such as a row index and a column index

    Explanation

    A two-dimensional array uses two indices to identify an element, commonly representing a row and a column.

  51. 51

    Which method is suited to producing a readable representation of a multidimensional array?

    1. A

      String.length

    2. B

      Arrays.deepToString

    3. C

      Arrays.sort

    4. D

      Arrays.toString

    Correct answer
    B

    Arrays.deepToString

    Explanation

    Arrays.deepToString is used to display nested, multidimensional array contents; Arrays.toString is for a single dimension.

  52. 52

    What is the role of an array’s length field?

    1. A

      It reports the index of the final element

    2. B

      It returns a string representation of the elements

    3. C

      It reports the number of elements the array can hold

    4. D

      It changes the array’s size

    Correct answer
    C

    It reports the number of elements the array can hold

    Explanation

    An array’s length field gives its number of elements; the last valid index is one less than that length.

  53. 53

    Which statement about Java array initialization is correct when the elements are already known?

    1. A

      An array must always be filled by assigning each index in a separate statement

    2. B

      An array can be initialized with its elements inside curly braces

    3. C

      An array can be initialized only using Arrays.toString

    4. D

      An array must be declared as a collection to list its initial elements

    Correct answer
    B

    An array can be initialized with its elements inside curly braces

    Explanation

    When the elements are known, Java permits a concise array initializer using curly braces and comma-separated values.

  54. 54

    What does the modulus operator % return?

    1. A

      The product of two values

    2. B

      The absolute value of a number

    3. C

      The quotient of a division

    4. D

      The remainder of a division

    Correct answer
    D

    The remainder of a division

    Explanation

    The modulus operator returns the remainder; it can be used to test whether a number is divisible by another number.

  55. 55

    Why does integer division of two whole numbers produce a whole-number result in Java?

    1. A

      An integer variable cannot hold the quotient, so Java returns zero

    2. B

      Java automatically rounds every quotient upward

    3. C

      The division operator returns a string for whole-number operands

    4. D

      The operation uses integer arithmetic, which discards the fractional part

    Correct answer
    D

    The operation uses integer arithmetic, which discards the fractional part

    Explanation

    When both operands are integers, integer division produces an integer result, so the fractional part is discarded.

  56. 56

    What is the result of evaluating 10 + 3 * 2 under the usual Java arithmetic precedence?

    1. A

      26, because addition is evaluated before multiplication

    2. B

      15, because multiplication is ignored in an integer expression

    3. C

      32, because the operators are evaluated from left to right

    4. D

      16, because multiplication is evaluated before addition

    Correct answer
    D

    16, because multiplication is evaluated before addition

    Explanation

    Multiplication has higher precedence than addition, so the expression is evaluated as 10+(3×2)=1610 + (3 \times 2) = 1610+(3×2)=16.

  57. 57

    What is the purpose of parentheses in an arithmetic expression?

    1. A

      They make every operator have equal precedence

    2. B

      They convert a reference type into a primitive type

    3. C

      They control evaluation order by grouping an expression

    4. D

      They mark the expression as a comment

    Correct answer
    C

    They control evaluation order by grouping an expression

    Explanation

    Parentheses have high precedence and can force a grouped expression to be evaluated before surrounding operations.

  58. 58

    How does postfix increment, as in y = x++, differ from prefix increment, as in y = ++x?

    1. A

      Postfix assigns the old value to y before incrementing x; prefix increments x before assigning to y

    2. B

      They always assign the same value to y and leave x unchanged

    3. C

      Postfix increments x twice; prefix increments x once

    4. D

      Postfix changes only y; prefix changes only x

    Correct answer
    A

    Postfix assigns the old value to y before incrementing x; prefix increments x before assigning to y

    Explanation

    With postfix increment, the expression yields the old value before incrementing. With prefix increment, x is incremented first and the new value is yielded.

  59. 59

    Which statement is equivalent to x = x + 2?

    1. A

      x++

    2. B

      x += 2

    3. C

      x = 2 + x++

    4. D

      x == 2

    Correct answer
    B

    x += 2

    Explanation

    The compound assignment x += 2 adds two to x and stores the result back in x.

  60. 60

    When does implicit numeric casting generally occur in the described rules?

    1. A

      Whenever converting a string to any numeric type

    2. B

      Whenever a variable is declared as final

    3. C

      Only when converting a larger type into a smaller type

    4. D

      When converting to a compatible type that can represent the value without losing data

    Correct answer
    D

    When converting to a compatible type that can represent the value without losing data

    Explanation

    Implicit casting occurs when Java can safely convert a value to a compatible type without a risk of data loss, such as widening from byte to int.

  61. 61

    Why does assigning a double expression to an int require explicit conversion?

    1. A

      The conversion can lose the digits after the decimal point

    2. B

      Java does not allow numeric types to be converted

    3. C

      A double is a reference type and cannot be assigned

    4. D

      An int uses more memory than a double

    Correct answer
    A

    The conversion can lose the digits after the decimal point

    Explanation

    Converting a floating-point value to an integer can discard its fractional part, so it requires an explicit cast when that loss is intended.

  62. 62

    Which approach converts a numeric string such as "42" into an integer?

    1. A

      Assign the string directly to an int variable

    2. B

      Use Integer.parseInt with the string

    3. C

      Cast the string to int with (int)

    4. D

      Use Math.round on the string

    Correct answer
    B

    Use Integer.parseInt with the string

    Explanation

    Integer.parseInt parses a string containing an integer representation and returns an int; a cast cannot convert a string to a number.

  63. 63

    What happens if a decimal string is passed to Integer.parseInt?

    1. A

      The method returns the number of digits in the string

    2. B

      Parsing fails because the string is not a valid integer representation

    3. C

      The method automatically returns a double

    4. D

      The decimal part is always rounded to the nearest integer

    Correct answer
    B

    Parsing fails because the string is not a valid integer representation

    Explanation

    Integer.parseInt expects an integer representation; a decimal value such as 1.1 causes a parsing exception.

  64. 64

    What does Math.ceil return for a positive non-integer value such as 1.1?

    1. A

      The nearest integer, represented as a long

    2. B

      A random integer greater than the value

    3. C

      The smallest integer greater than or equal to the value, represented as a double

    4. D

      The largest integer less than or equal to the value, represented as an int

    Correct answer
    C

    The smallest integer greater than or equal to the value, represented as a double

    Explanation

    Math.ceil rounds upward to the smallest integer greater than or equal to its argument; its result is a double.

  65. 65

    Which description correctly distinguishes Math.floor from Math.ceil for a value such as 1.1?

    1. A

      floor rounds to the nearest integer; ceil always returns zero

    2. B

      Both methods always return the same result for non-integers

    3. C

      floor returns a random lower value; ceil returns a random higher value

    4. D

      floor gives the largest integer no greater than the value; ceil gives the smallest integer no less than it

    Correct answer
    D

    floor gives the largest integer no greater than the value; ceil gives the smallest integer no less than it

    Explanation

    For 1.11.11.1, Math.floor gives 1.01.01.0 and Math.ceil gives 2.02.02.0; the two methods round in opposite directions.

  66. 66

    What is the role of Math.random() in the described examples?

    1. A

      It returns a floating-point value between 0 and 1, which can be scaled by multiplication

    2. B

      It returns the same fixed value every time the program runs

    3. C

      It rounds an input number to the nearest integer

    4. D

      It returns an integer between 1 and 100 by default

    Correct answer
    A

    It returns a floating-point value between 0 and 1, which can be scaled by multiplication

    Explanation

    Math.random() returns a double in the range from 000 up to, but not including, 111; multiplying it by 100100100 scales the range to 000 through values below 100100100.

  67. 67

    What is the purpose of obtaining a currency-specific NumberFormat instance?

    1. A

      It converts a currency string into a Java class

    2. B

      It performs arithmetic on currency values without returning a result

    3. C

      It guarantees that a numeric value has no decimal digits

    4. D

      It formats a number as currency, including currency symbols and localized separators

    Correct answer
    D

    It formats a number as currency, including currency symbols and localized separators

    Explanation

    A currency-specific NumberFormat formats values as currency, such as adding a currency symbol and grouping digits.

  68. 68

    Why can’t code instantiate the NumberFormat class directly with new in the described example?

    1. A

      It is abstract, so a factory method such as getCurrencyInstance is used to obtain an instance

    2. B

      It is a package name rather than a class

    3. C

      It is a primitive type, so it has no constructor

    4. D

      It is automatically instantiated only when a number is printed

    Correct answer
    A

    It is abstract, so a factory method such as getCurrencyInstance is used to obtain an instance

    Explanation

    An abstract class cannot be instantiated directly; a factory method such as getCurrencyInstance returns a suitable NumberFormat instance.

  69. 69

    What does a percent-specific NumberFormat do to a value of 0.1?

    1. A

      It formats the value as 0.1%

    2. B

      It changes the stored numeric value to 10

    3. C

      It converts the value to the integer 10

    4. D

      It formats the value as 10%

    Correct answer
    D

    It formats the value as 10%

    Explanation

    Percent formatting displays a decimal proportion as a percentage, so 0.10.10.1 is formatted as 10%10\%10%.

  70. 70

    What is method chaining?

    1. A

      Using one method to compile another method

    2. B

      Declaring multiple methods inside a package

    3. C

      Calling the same method repeatedly without arguments

    4. D

      Calling a method on the value returned by another method

    Correct answer
    D

    Calling a method on the value returned by another method

    Explanation

    Method chaining uses the return value of one method call as the receiver for a subsequent method call.

  71. 71

    What must be supplied to a Scanner when reading from the terminal in the described example?

    1. A

      A NumberFormat instance

    2. B

      System.in

    3. C

      The name of the main class

    4. D

      System.out

    Correct answer
    B

    System.in

    Explanation

    System.in is the input stream supplied to a Scanner to read data from the terminal.

  72. 72

    What is the difference between System.in and System.out?

    1. A

      System.in is used for input, while System.out is used for output

    2. B

      System.in compiles code, while System.out runs bytecode

    3. C

      System.in is a method, while System.out is a package

    4. D

      System.in stores strings, while System.out stores arrays

    Correct answer
    A

    System.in is used for input, while System.out is used for output

    Explanation

    System.in is the standard input stream; System.out is the standard output stream used for terminal output.

  73. 73

    Why does Scanner.next() usually return only the first part of an input such as a full name?

    1. A

      It rejects all text containing letters

    2. B

      It reads one token, and whitespace separates tokens

    3. C

      It reads only the first character of each line

    4. D

      It automatically trims and deletes the second word

    Correct answer
    B

    It reads one token, and whitespace separates tokens

    Explanation

    next() reads one token, with whitespace acting as a separator; nextLine() reads the entire line.

  74. 74

    Why is nextLine() appropriate for reading a full name with spaces?

    1. A

      It reads the entire line, including multiple whitespace-separated words

    2. B

      It reads a line only when the input is numeric

    3. C

      It automatically converts the name to a character array

    4. D

      It reads only the first word but preserves its spaces

    Correct answer
    A

    It reads the entire line, including multiple whitespace-separated words

    Explanation

    Unlike next(), nextLine() reads the entire line entered by the user, including spaces between words.

  75. 75

    Why is it generally preferable to create a Scanner once outside a repeated input loop?

    1. A

      It avoids repeatedly creating unnecessary scanner objects

    2. B

      It changes the loop condition into a boolean

    3. C

      It makes the input automatically validate itself

    4. D

      It causes nextLine() to read only one token

    Correct answer
    A

    It avoids repeatedly creating unnecessary scanner objects

    Explanation

    Creating a new scanner on every iteration is unnecessary; reusing one avoids repeatedly creating objects.

  76. 76

    In a mortgage calculator, how is an annual interest rate expressed as a percentage converted to a monthly decimal rate?

    1. A

      Subtract 12 from the annual percentage and divide by 100

    2. B

      Multiply the annual percentage by 100 and then by 12

    3. C

      Divide the annual percentage by 12 and then multiply by 100

    4. D

      Divide the annual percentage by 100 and then by 12

    Correct answer
    D

    Divide the annual percentage by 100 and then by 12

    Explanation

    First divide by 100100100 to convert a percentage to a decimal rate, then divide by 121212 to obtain a monthly rate.

  77. 77

    If a mortgage payment is calculated over a term measured in years, how is the number of monthly payments obtained?

    1. A

      Divide the number of years by 12

    2. B

      Add 12 to the number of years

    3. C

      Multiply the number of years by 100

    4. D

      Multiply the number of years by 12

    Correct answer
    D

    Multiply the number of years by 12

    Explanation

    There are 121212 monthly payments per year, so the number of payments is the number of years multiplied by 121212.

  78. 78

    Why should a mortgage calculator validate input values before calculating a payment?

    1. A

      Validation makes the formula unnecessary

    2. B

      Validation guarantees every user enters the same loan amount

    3. C

      Invalid or nonnumeric input can cause incorrect behavior or a program error

    4. D

      Invalid input is automatically converted to a valid value by Java

    Correct answer
    C

    Invalid or nonnumeric input can cause incorrect behavior or a program error

    Explanation

    Nonnumeric or out-of-range values can cause errors or produce inappropriate results, so the program should validate input and prompt again when needed.

  79. 79

    How can a program keep asking for a value until it falls within an acceptable range?

    1. A

      Use a loop that repeats the prompt until validation succeeds, then break out

    2. B

      Use a switch statement that runs only once

    3. C

      Use a comment to prevent invalid values from being entered

    4. D

      Use a single assignment statement to replace invalid input automatically

    Correct answer
    A

    Use a loop that repeats the prompt until validation succeeds, then break out

    Explanation

    A repeated loop can prompt for input, check whether it is valid, and use break once the value passes validation.

  80. 80

    Why should a program use named constants instead of unexplained numeric literals for values such as months per year?

    1. A

      Named constants explain the meaning of values and help avoid magic numbers

    2. B

      Named constants make numeric types compatible with strings

    3. C

      Named constants allow variables to change their values more often

    4. D

      Named constants remove the need for input validation

    Correct answer
    A

    Named constants explain the meaning of values and help avoid magic numbers

    Explanation

    A named constant such as MONTHS_IN_YEAR makes code more understandable than an unexplained literal such as 12.

  81. 81

    What does the equality operator == test when used with primitive values?

    1. A

      Whether the first value is greater than the second

    2. B

      Whether the values are equal

    3. C

      Whether the values are assigned to each other

    4. D

      Whether the values belong to the same package

    Correct answer
    B

    Whether the values are equal

    Explanation

    The equality operator == compares values; a single = is the assignment operator.

  82. 82

    What result does the logical AND operator produce when at least one of its two conditions is false?

    1. A

      true

    2. B

      The value of the left condition only

    3. C

      false

    4. D

      A syntax error

    Correct answer
    C

    false

    Explanation

    Logical AND is true only when both conditions are true, so it is false if either condition is false.

  83. 83

    What result does the logical OR operator produce when at least one condition is true?

    1. A

      The value of the last condition only

    2. B

      An integer representing the number of true conditions

    3. C

      false

    4. D

      true

    Correct answer
    D

    true

    Explanation

    Logical OR is true when at least one of its conditions is true.

  84. 84

    What does the logical NOT operator do to a boolean value?

    1. A

      It assigns a value to a variable

    2. B

      It reverses the value from true to false or from false to true

    3. C

      It combines two conditions into one

    4. D

      It checks whether two strings have equal text

    Correct answer
    B

    It reverses the value from true to false or from false to true

    Explanation

    Logical NOT negates a boolean value, reversing true and false.

  85. 85

    Why is an else if condition unnecessary when it is logically implied by the failure of an earlier condition?

    1. A

      An else if branch always runs after an if branch

    2. B

      The compiler automatically converts every else if to a loop

    3. C

      The earlier condition’s failure already establishes the fact needed for that branch

    4. D

      Java does not permit else if conditions

    Correct answer
    C

    The earlier condition’s failure already establishes the fact needed for that branch

    Explanation

    For example, if the only alternatives are a temperature above 303030 and then a remaining case, failure of the first test already implies it is no greater than 303030.

  86. 86

    When is the ternary operator useful?

    1. A

      When comparing a value against many unrelated classes

    2. B

      When repeating a block an unknown number of times

    3. C

      When selecting one of two values based on a condition and assigning the result

    4. D

      When creating an array with a variable length

    Correct answer
    C

    When selecting one of two values based on a condition and assigning the result

    Explanation

    The ternary operator evaluates a condition and yields one value if it is true and another if it is false, which is useful for concise conditional assignments.

  87. 87

    What is the role of a default clause in a switch statement?

    1. A

      It provides code to execute when none of the listed cases matches

    2. B

      It declares a default value for every variable in the program

    3. C

      It exits the switch before any case is checked

    4. D

      It marks the first case that must always execute

    Correct answer
    A

    It provides code to execute when none of the listed cases matches

    Explanation

    The default clause runs when none of the switch’s case values matches the expression.

  88. 88

    Why are cases in a Java switch commonly followed by break?

    1. A

      To declare the value being compared

    2. B

      To prevent the case expression from being evaluated

    3. C

      To restart evaluation from the first case

    4. D

      To exit the switch instead of continuing into subsequent case code

    Correct answer
    D

    To exit the switch instead of continuing into subsequent case code

    Explanation

    A break exits the switch. Without it, execution can continue into statements associated with later cases.

  89. 89

    Why should the most specific condition be checked before more general conditions in a sequence of if and else if branches?

    1. A

      Specific conditions are always slower to evaluate

    2. B

      A specific condition cannot appear after an else if

    3. C

      Java requires conditions to be ordered alphabetically

    4. D

      A general condition might otherwise match first and prevent the specific branch from running

    Correct answer
    D

    A general condition might otherwise match first and prevent the specific branch from running

    Explanation

    In a first-match conditional chain, a broad condition may be true before a more specific one, so the specific case should be tested first.

  90. 90

    What distinguishes a for loop from a while loop in the examples?

    1. A

      A for loop is used only for strings, while a while loop is used only for numbers

    2. B

      A for loop can run only once, while a while loop runs forever

    3. C

      A while loop always has a counter, while a for loop never does

    4. D

      A for loop groups initialization, condition, and update; a while loop separates them

    Correct answer
    D

    A for loop groups initialization, condition, and update; a while loop separates them

    Explanation

    A for loop places the loop variable initialization, continuation condition, and update in its header; a while loop typically keeps initialization and updating elsewhere.

  91. 91

    When is a for loop especially suitable?

    1. A

      When the number of repetitions is known or can be controlled with a counter

    2. B

      When the loop must always execute at least once

    3. C

      When the program must stop as soon as it reads any input

    4. D

      When iterating over a string without examining its characters

    Correct answer
    A

    When the number of repetitions is known or can be controlled with a counter

    Explanation

    A for loop is convenient when the repetition count is known or can be controlled by a loop counter.

  92. 92

    When is a while loop especially suitable?

    1. A

      When the program has no condition to test

    2. B

      When the number of repetitions is not known in advance and depends on a condition

    3. C

      When a block must be executed exactly once

    4. D

      When an array’s elements must be printed without any condition

    Correct answer
    B

    When the number of repetitions is not known in advance and depends on a condition

    Explanation

    A while loop is useful when repetition depends on a condition, such as continuing to prompt until the user enters quit.

  93. 93

    What is the key execution difference between a do-while loop and a while loop?

    1. A

      A do-while loop checks its condition before the body, so it may never run

    2. B

      A do-while loop iterates only over arrays

    3. C

      A do-while loop cannot contain a break statement

    4. D

      A do-while loop checks its condition after the body, so it runs at least once

    Correct answer
    D

    A do-while loop checks its condition after the body, so it runs at least once

    Explanation

    A do-while loop checks its condition after executing the body, so it executes at least once even if the condition is initially false.

  94. 94

    What does the break statement do inside a loop?

    1. A

      It terminates the loop

    2. B

      It skips only the current iteration and proceeds to the next one

    3. C

      It restarts the loop from its initialization

    4. D

      It pauses the program until the user enters a value

    Correct answer
    A

    It terminates the loop

    Explanation

    break exits the loop immediately.

  95. 95

    What does the continue statement do inside a loop?

    1. A

      It skips the remaining statements in the current iteration and proceeds to the next iteration

    2. B

      It terminates the loop permanently

    3. C

      It restarts the whole program

    4. D

      It exits the method that contains the loop

    Correct answer
    A

    It skips the remaining statements in the current iteration and proceeds to the next iteration

    Explanation

    continue moves control to the next iteration, skipping the rest of the current iteration’s body.

  96. 96

    Why must a loop written as while (true) have a suitable exit mechanism?

    1. A

      Otherwise, the loop body is never executed

    2. B

      Otherwise, the condition changes automatically to false

    3. C

      Otherwise, the compiler converts it into a for loop

    4. D

      Otherwise, the loop may continue indefinitely

    Correct answer
    D

    Otherwise, the loop may continue indefinitely

    Explanation

    The condition true never becomes false by itself, so the loop needs an exit such as a break statement to avoid running forever.

  97. 97

    What is a limitation of a for-each loop compared with a traditional indexed for loop?

    1. A

      It cannot iterate over arrays

    2. B

      It does not directly provide the index of each item

    3. C

      It can iterate only from the end toward the beginning

    4. D

      It requires a separate counter to access each item

    Correct answer
    B

    It does not directly provide the index of each item

    Explanation

    A for-each loop provides each element’s value but not its index; an indexed for loop makes the index available.

  98. 98

    Which statement about for-each iteration is correct in the described examples?

    1. A

      It changes the fixed length of the array as it iterates

    2. B

      It is appropriate only when the array’s index is required

    3. C

      It moves forward through the elements and is convenient when only each value is needed

    4. D

      It can move both forward and backward without additional code

    Correct answer
    C

    It moves forward through the elements and is convenient when only each value is needed

    Explanation

    A for-each loop conveniently visits array elements in forward order when the index is not needed; a traditional loop offers more control over direction and indexing.

  99. 99

    Why can extracting code into smaller methods improve a long main method?

    1. A

      It removes the need to choose appropriate variable types

    2. B

      It can make the program easier to read, understand, and maintain

    3. C

      It makes all methods accessible from every class

    4. D

      It guarantees that the program uses less memory

    Correct answer
    B

    It can make the program easier to read, understand, and maintain

    Explanation

    Breaking a long method into smaller, focused chunks improves readability and maintainability by making the program’s parts easier to understand.

  100. 100

    What does the programming principle DRY stand for?

    1. A

      Direct runtime yield

    2. B

      Declare references yearly

    3. C

      Don’t repeat yourself

    4. D

      Data returns yesterday

    Correct answer
    C

    Don’t repeat yourself

    Explanation

    DRY means “Don’t repeat yourself”: avoid unnecessary duplication of code or logic.

  101. 101

    Why might a programmer choose a slightly repetitive flat conditional over a nested conditional?

    1. A

      Repetition makes the program execute fewer instructions

    2. B

      A flat conditional automatically validates user input

    3. C

      The flat structure may be easier to read, even if it repeats some logic

    4. D

      Nested conditionals are always invalid Java

    Correct answer
    C

    The flat structure may be easier to read, even if it repeats some logic

    Explanation

    Programming solutions involve trade-offs. A flat structure with some repetition can be clearer than nested conditionals that are harder to follow.

Question 1 of 101
Question 1 of 101

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent