EzDevInfo.com

boolean interview questions

Top boolean frequently asked interview questions

Check if at least two out of three booleans are true

An interviewer recently asked me this question: given three boolean variables, a, b, and c, return true if at least two out of the three are true.

My solution follows:

boolean atLeastTwo(boolean a, boolean b, boolean c) {
    if ((a && b) || (b && c) || (a && c)) {
        return true;
    }
    else{
        return false;
    }
}

He said that this can be improved further, but how?


Source: (StackOverflow)

Using boolean values in C

C doesn't have any built in boolean types. What's the best way to use them in C?


Source: (StackOverflow)

Advertisements

Strange definitions of TRUE and FALSE macros

I have seen the following macro definitions in a coding book.

#define TRUE  '/'/'/'
#define FALSE '-'-'-'

There was no explanation.

Please explain to me how these will work as TRUE and FALSE.


Source: (StackOverflow)

What is the difference between bool and Boolean types in C#

What is the difference between bool and Boolean types in C#?


Source: (StackOverflow)

How do I use boolean variables in Perl?

I have tried:

$var = false;
$var = FALSE;
$var = False;

None of these work. I get the error message

Bareword "false" not allowed while "strict subs" is in use.

Source: (StackOverflow)

Convert boolean to int in Java

What is the most accepted way to convert a boolean to an int in Java?


Source: (StackOverflow)

Which MySQL Datatype to use for storing boolean values?

Since MySQL doesn't seem to have any 'boolean' datatype, which datatype do you 'abuse' for storing true/false information in MySQL? Especially in the context of writing and reading from/to a PHP-Script.

Over time I have used and seen several approaches:

  • tinyint, varchar fields containing the values 0/1,
  • varchar fields containing the strings '0'/'1' or 'true'/'false'
  • and finally enum Fields containing the two options 'true'/'false'.

None of the above seems optimal, I tend to prefer the tinyint 0/1 variant, since automatic type conversion in PHP gives me boolean values rather simply.

So which datatype do you use, is there a type designed for boolean values which I have overlooked? Do you see any advantages/disadvantages by using one type or another?


Source: (StackOverflow)

Volatile boolean vs AtomicBoolean

What does AtomicBoolean do that a volatile boolean cannot achieve?


Source: (StackOverflow)

In Javascript, why is "0" equal to false, but when tested by 'if' it is not false by itself?

The following shows that "0" is false in Javascript:

>>> "0" == false
true

>>> false == "0"
true

So why does the following print "ha"?

>>> if ("0") console.log("ha")
ha

Source: (StackOverflow)

Get boolean from database using Android and SQLite

How can I obtain the value of a boolean field in an SQLite database on Android?

I usually use getString(), getInt(), etc. to get the values of my fields, but there does not seem to be a getBoolean() method.


Source: (StackOverflow)

Is the operation "false < true" well defined?

Does the C++ specification define:

  1. the existence of the 'less than' operator for boolean parameters, and if so,
  2. the result of the 4 parameter permutations?

In other words, are the results from the following operations defined by the specification?

false < false
false < true
true < false
true < true

On my setup (Centos 7, gcc 4.8.2) , the code below spits out what I'd expect (given C's history of representing false as 0 and true as 1):

false < false = false
false < true = true
true < false = false
true < true = false

Whilst I'm pretty sure most (all?) compilers will give the same output, is this legislated by the C++ specification? Or is an obfuscating, but specification-compliant compiler allowed to decide that true is less than false?

#include <iostream>

const char * s(bool a)
{
  return (a ? "true" : "false");
}

void test(bool a, bool b)
{
  std::cout << s(a) << " < " << s(b) << " = " << s(a < b) << std::endl;
}

int main(int argc, char* argv[])
{
  test(false, false);
  test(false, true);
  test(true, false);
  test(true, true);
  return 0;
}

Source: (StackOverflow)

When should null values of Boolean be used?

Java boolean allows values of true and false while Boolean allows true, false, and null. I have started to convert my booleans to Booleans. This can cause crashes in tests such as

Boolean set = null;
...
if (set) ...

while the test

if (set != null && set) ...

seems contrived and error-prone.

When, if ever, is it useful to use Booleans with null values? If never, then what are the main advantages of the wrapped object?

UPDATE: There has been such a lot of valuable answers that I have summarised some of it in my own answer. I am at best an intermediate in Java so I have tried to show the things that I find useful. Note that the question is "incorrectly phrased" (Boolean cannot "have a null value") but I have left it in case others have the same misconception


Source: (StackOverflow)

Is there a difference between YES/NO,TRUE/FALSE and true/false in objective-c?

Simple question really; is there a difference between these values (and is there a difference between BOOL and bool)? A co-worker mentioned that they evaluate to different things in Objective-C, but when I looked at the typedefs in their respective .h files, YES/TRUE/true were all defined as 1 and NO/FALSE/false were all defined as 0. Is there really any difference?


Source: (StackOverflow)

Is False == 0 and True == 1 in Python an implementation detail or is it guaranteed by the language?

Is it guaranteed that False == 0 and True == 1, in Python? For instance, is it in any way guaranteed that the following code will always produce the same results, whatever the version of Python (both existing and, likely, future ones)?

0 == False  # True
1 == True   # True
['zero', 'one'][False]  # is 'zero'

Any reference to the official documentation would be much appreciated!

Edit: As noted in many answers, bool inherits from int. The question can therefore be recast as: "Does the documentation officially say that programmers can rely on booleans inheriting from integers, with the values 0 and 1?". This question is relevant for writing robust code that won't fail because of implementation details!


Source: (StackOverflow)

Booleans, conditional operators and autoboxing

Why does this throw NullPointerException

public static void main(String[] args) throws Exception {
    Boolean b = true ? returnsNull() : false; // NPE on this line.
    System.out.println(b);
}

public static Boolean returnsNull() {
    return null;
}

while this doesn't

public static void main(String[] args) throws Exception {
    Boolean b = true ? null : false;
    System.out.println(b); // null
}

?

The solution is by the way to replace false by Boolean.FALSE to avoid null being unboxed to boolean --which isn't possible. But that isn't the question. The question is why? Are there any references in JLS which confirms this behaviour, especially of the 2nd case?


Source: (StackOverflow)