Java Variables and Boolean Logic Quiz
18 Questions
1 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

For the expression a == b == c to be valid in Java, what must the data type of c be?

  • `double`
  • `int`
  • `boolean` (correct)
  • `String`
  • Given three boolean variables a, b, and c, and the expression !a || b && c, under which condition is the entire expression guaranteed to evaluate to true?

  • When `a` is `true`, `b` is `true`, and `c` is `false`.
  • When `a` is `false`, and `b` and `c` are both `false`.
  • When `a` is `false` and `b` and `c` are both `true`. (correct)
  • When `a` is `true`, and `b` and `c` are both `false`.
  • What is the final value of a printed to the console in the following Java code snippet?

  • 1
  • 2 (correct)
  • -1
  • 0
  • What are the values of variables c and d after executing the following Java code?

    <p><code>c = 11</code>, <code>d = 20</code> (B)</p> Signup and view all the answers

    How many times will the following for loop execute?

    <p>Infinite (A)</p> Signup and view all the answers

    What is the result of the expression (1 == 3) == (4 == 5) in Java?

    <p><code>true</code> (B)</p> Signup and view all the answers

    Which of the following Java types is a primitive type?

    <p><code>int</code> (B)</p> Signup and view all the answers

    If b is a variable with an unknown type, which data type assignment to variable b would cause compilation errors in the code double a = b;?

    <p><code>String</code> (B)</p> Signup and view all the answers

    What is the primary consequence of omitting a break statement within a switch statement's case block?

    <p>Execution will continue into the subsequent <code>case</code> blocks until a <code>break</code> is encountered or the end of the <code>switch</code> statement is reached. (A)</p> Signup and view all the answers

    Given a two-dimensional array tab where rows may have different lengths, what is a correct approach to calculate the total number of elements in the array?

    <p>Nest loops, with the outer loop iterating over rows, and the inner loop summing the lengths of each row. (A)</p> Signup and view all the answers

    How do you determine the appropriate index for storing or retrieving elements in a HashMap?

    <p>By using the <code>hashCode()</code> method of the key, and then mapping the result to the table size, often using a modulo operation. (D)</p> Signup and view all the answers

    What is the fundamental distinction between a class and an object in object-oriented programming?

    <p>A class is a blueprint or template for creating objects, while an object is an instance of a class. (A)</p> Signup and view all the answers

    In Java, what interface should replace *** in public static void metoda(T nizi) such that the enhanced for loop for (String niz : nizi) compiles correctly?

    <p>extends Iterable (B)</p> Signup and view all the answers

    Which option describes how custom ordering is implemented?

    <p>A Comparator must use a separate (static) class that implements <code>Comparator</code>. (C)</p> Signup and view all the answers

    What is the crucial difference between the List and Set interfaces in Java's Collections Framework?

    <p><code>List</code> maintains the order of elements and allows duplicates, while <code>Set</code> does not allow duplicates and does not guarantee element order. (A)</p> Signup and view all the answers

    Given the options ArrayList and LinkedList, which statement represents a key difference between the two?

    <p><code>ArrayList</code> uses a dynamic array, while <code>LinkedList</code> employs a doubly linked list. (A)</p> Signup and view all the answers

    What will the map.size() method return after executing the following code?

    <p>1 (A)</p> Signup and view all the answers

    If sorting a List using a lambda expression with compareTo(), what change is needed to sort it in the opposite (descending) order?

    <p>Use <code>(a, b) -&gt; b.compareTo(a)</code>. (D)</p> Signup and view all the answers

    Flashcards

    a == b == c conditions

    For a == b == c, a and b can be int or any type, but c must be boolean.

    !a || b && c true cases

    !a || b && c is true in specific combinations of a, b, and c.

    do-while execution example

    The do-while loop runs at least once; here it prints 1 and stops when a < 0.

    Infinite loop scenario

    The for loop with condition 'int i = 1; i > 0' creates an infinite loop.

    Signup and view all the flashcards

    String comparison results

    'prva' == 'prva' is true, while 'prva' vs new String('prva') is false due to reference.

    Signup and view all the flashcards

    Primitive vs Reference Types

    'a' is a char (primitive), while 'a' is a String (reference).

    Signup and view all the flashcards

    Arithmetic with ASCII

    '1' + 2 returns 51 because '1' is ASCII 49.

    Signup and view all the flashcards

    Integer multiplication

    Integer multiplication compiles to Integer.valueOf(a) * Integer.valueOf(b) to convert to int.

    Signup and view all the flashcards

    Equality in Java

    "1".equals(2) compares differently than expected; uses String and Integer.

    Signup and view all the flashcards

    Break Statement

    Using break exits a loop or switch; forgetting it runs all cases.

    Signup and view all the flashcards

    2D Array Size

    Use nested loops to count elements in a 2D array.

    Signup and view all the flashcards

    Objects vs Object Variables

    An object is the actual data; an object variable references it.

    Signup and view all the flashcards

    Comparable vs Comparator

    Comparable uses compareTo for natural order; Comparator uses compare for custom order.

    Signup and view all the flashcards

    Iterable Implementation

    Implement Iterable by defining an iterator() method.

    Signup and view all the flashcards

    Hash Table Basics

    Keys are hashed for index; collisions handled with linked lists.

    Signup and view all the flashcards

    List vs Set

    List allows duplicates and maintains order; Set does not.

    Signup and view all the flashcards

    Lambda Expressions

    Used for sorting Lists; (a, b) standard for compare operations.

    Signup and view all the flashcards

    TreeMap vs HashMap

    TreeMap is ordered; HashMap is unordered.

    Signup and view all the flashcards

    Study Notes

    Variable Comparisons

    • For a == b == c, c must be a boolean type.
    • Possible combinations for a, b, c to return true include:
      • a, b, c = true
      • a, b = false, c = true
      • a = true, b, c = false

    Boolean Expression Evaluation

    • !a || b && c is true in the following scenarios:
      • a, b, c = true
      • a = true, b, c = false
      • a = false, b, c = true
      • a = true, b, c = false
      • a, b = false, c = true

    Code Output Example

    • The Java code int a = 1; do { System.out.println(a); a++; } while (a < 0); System.out.println(a); outputs:
      • 1
      • 2

    Variable Modification

    • Given int a = 10; int b = 20; int c = ++a; int d = b++;, the values after execution are:
      • a = 11
      • b = 21
      • c = 11
      • d = 20

    Loop Execution Analysis

    • for (int i = 1; i > 0; i++) {...} creates an infinite loop.

    Comparison Result

    • 1 > 2 evaluates to false.

    Compound Comparison

    • (1 == 3) == (4 == 5) evaluates to true (because false == false equals true).

    Primitive vs. Reference Types

    • int is a primitive type.

    • int[] is a reference type.

    • Integer is a reference type.

    • Integer[] is a reference type.

    • Primitive types hold values directly, while reference types hold references to objects in memory.

    ASCII Code

    • The ASCII code for '1' is 49.

    Character Arithmetic

    • '1' + 2 results in 51 (ASCII value of '1' + 2).
    • '5' - '3' results in 2 (ASCII value of '5' - ASCII value of '3').

    String Comparisons

    • "prva" == "prva" returns true because it compares references to the same string literal.
    • "prva" == new String("prva") returns false because it compares references (one is the literal, the other is a newly created String object).
    • new String("prva") == new String("prva") returns false because it compares references to two different String objects.

    Char vs. String

    • 'a' is a char (primitive).
    • "a" is a String (reference).

    Type Compatibility

    • In the expression double a = b;, b can be of type double, int, or byte.

    Compiler Translation

    • Integer a = 2; Integer b = 3; System.out.print(a * b); is translated to Integer.valueOf(a) * Integer.valueOf(b) (which automatically converts to int for multiplication).

    Compile-Time Error

    • "1".equals(2) compiles because it translates to String.valueOf("1").equals(Integer.valueOf(2)).

    Loop Alternatives

    • break in loops can almost always be replaced by a conditional expression in the loop condition.

    Switch Statement Issues

    • Forgetting break in a switch statement causes execution to continue into subsequent case blocks, affecting the expected control flow.

    2D Array Size Determination

    • For a 2D array (e.g., int[][] tab), to determine the total number of elements use nested loops:
      int count = 0;
      for (int i = 0; i < tab.length; i++) {
          for (int j = 0; j < tab[i].length; j++) {
              count++;
          }
      }
      
    • To get the length of a row tab[i].length.

    Hash Table Operations

    • Hash tables use hashing (hashCode()) to determine indices, managing collisions using linked lists (chaining). Elements are searched using equals().

    List vs. Set

    • List allows duplicate elements and maintains insertion order.
    • Set does not allow duplicate elements and does not maintain insertion order.

    Collection Implementations

    • List, Set, and Queue are classes that implement Collection.

    ArrayList vs. LinkedList

    • ArrayList uses a dynamic array; LinkedList uses a doubly linked list. This largely impacts performance for operations like insertion or removal in the middle of the list.

    TreeMap vs. HashMap

    • TreeMap stores elements in a sorted order; HashMap stores elements in no particular order.

    TreeSet Size

    • TreeSet map = new TreeSet(); map.add(10); map.add(10); map.add(20); map.add(30); map.size(); will print 3 because elements are added only once.

    Iterable and For-Each

    • For for-each loops to work on a container, the container must implement Iterable.

    Lambda Sort Parameter

    • When using a lambda to sort a List, the parameters (a, b) in the lambda (within a Comparator or compareTo) must be the type of elements in the list (integer in this case).

    Lambda Sorting in reverse

    • To sort in reverse order, in a lambda (a, b) -> b.compareTo(a) is used instead of (a, b) -> a.compareTo(b).

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Test your knowledge of variable comparisons and boolean expressions in Java. This quiz covers concepts such as truth conditions for boolean expressions and variable modifications. Perfect for Java beginners looking to enhance their programming skills!

    More Like This

    Java Programming Vocabulary Quiz
    20 questions
    Java Programming Chapters 1-3 Quiz
    78 questions
    Java Programming Chapter 11 Quiz
    8 questions

    Java Programming Chapter 11 Quiz

    WellConnectedComputerArt avatar
    WellConnectedComputerArt
    Use Quizgecko on...
    Browser
    Browser