Learn AP Comp Sci

Problem of the Day

Wednesday, December 31, 2025


Problem:

The boolean variable social is set to true when the user feels like being around other people, and otherwise is false. And the boolean variable active is set to true when the user feels like a more physical activity, but is otherwise false. A user who is feeling like being active might "play soccer with friends" or "hike alone"; otherwise, they might "hang out at cafe with friends" or "read (alone)". Which series of boolean statements will print the correct activity for a user based on the values of social and active?


  1. if (!active)
    if (social)
    System.out.println("hang out at cafe with friends");
    else
    System.out.println("read (alone)");
    else
    if (social)
    System.out.println("go hiking alone");
    else
    System.out.println("play soccer with friends");

  2. if (!active)
    if (social)
    System.out.println("hang out at cafe with friends");
    else
    System.out.println("read (alone)");
    else
    if (!social)
    System.out.println("go hiking alone");
    else
    System.out.println("play soccer with friends");

  3. if (active)
    if (social)
    System.out.println("hang out at cafe with friends");
    else
    System.out.println("go hiking alone");
    else
    if (social)
    System.out.println("play soccer with friends");
    else
    System.out.println("read (alone)");

  4. if (social)
    if (active)
    System.out.println("hang out at cafe with friends");
    else
    System.out.println("go hiking alone");
    else
    if (active)
    System.out.println("play soccer with friends");
    else
    System.out.println("read (alone)");

Show solution:

The correct answer is b. That solution is the only one with the correct logic. Note that the inner if statements for this solution are inconsistently presented: the first one checks if (social) while the second one check if (!social). While there is nothing wrong with the logic of this code, from a programming perspective it makes more sense to maintain some consistency in the statements' structure. This can be considered a form of defensive programming, where good practices can help avoid mistakes.