A boolean is a data type that can only have two possible values: true or false. It is often used in programming to make decisions and control the flow of a program.
Think of a light switch that can be either on (true) or off (false). Just like how you use a light switch to determine whether the lights are on or off, a boolean variable helps programmers determine whether certain conditions are true or false.
Condition: A condition is an expression that evaluates to either true or false. It is used in programming to make decisions and control the flow of code.
Boolean operators: Boolean operators are symbols such as AND, OR, and NOT that allow you to combine multiple conditions together in order to create more complex expressions.
Control flow: Control flow refers to the order in which statements are executed in a program. Booleans play a crucial role in controlling the flow of code by determining which statements should be executed based on certain conditions.
Consider the following method: public void printPrimeNumbers(int n) { for (int i = 2; i <= n; i++) { boolean isPrime = true; for (int j = 2; j <= Math.sqrt(i); j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) { System.out.print(i + " "); } } } Assume that the method printPrimeNumbers(20) appears in a method in the same class. What is printed as a result of the method call?
Consider the following method. public boolean isValidEmail(String email) { /* implementation not shown */ } Which of the following lines of code, if located in a method in the same class as isValidEmail, will compile without an error?
Consider the following method: public static boolean isPalindrome(String word) { int length = word.length(); for (int i = 0; i < length / 2; i++) { if (word.charAt(i) != word.charAt(length - 1 - i)) { return false; } } return true; } Assume that the following code segment appears in a class: boolean result = isPalindrome("racecar"); System.out.println("Is 'racecar' a palindrome? " + result); What is printed as a result of executing the code segment?
Consider the following method: public static boolean isEven(int number) { return number % 2 == 0; } Assume that the following code segment appears in a class: boolean result = isEven(7); System.out.println("Is 7 an even number? " + result); What is printed as a result of executing the code segment?
How many parameters does the method with header "public void doSomething (double a, double b, int c, boolean flag, String text)" have?
What does the following code do? public static boolean codeBlock(int[] array) { for (int i = 0; i < array.length - 1; i++) { for (int j = i + 1; j < array.length; j++) { if (array[j] == array[i]) { return true; } } } return false; }
What does the following code do? public static boolean codeBlock(int[] array) { for (int i: array) { if (i >= 25) { return false; } } return true; }
What does the following code do? public static boolean codeBlock(ArrayList<Integer> array) { for (int i: array) { if (i % 3 == 1 || i % 3 == 2) { return false; } } return true; }
Which statement correctly initializes an array of type `boolean` named `flags` with the elements `true`, `false`, and `true` in that order?
Study guides for the entire semester
200k practice questions
Glossary of 50k key terms - memorize important vocab
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.