1. Given the following code declaring and initializing three boolean variables a
ID: 3677695 • Letter: 1
Question
1. Given the following code declaring and initializing three boolean variables a, b, and c, indicate whether the value of each expression is true or false. (3 pts. each) boolean a true boolean b false boolean c true a && b a & & b) a & & b a l I c a II b 66 c) 2. Given the following code declaring and initializing two int variables a and b, indicate whether the value of each expression is true or false. (3 pts. each) int a 3; int b 5; a b II a 10 a b && b 10 4 II b a a 2 & & b a.Explanation / Answer
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
1)
truth table for AND
a
b
a&&b
True
true
True
true
false
False
false
true
False
false
false
false
truth table for OR
a
b
a&&b
True
True
True
True
False
True
False
True
True
False
False
False
! -- denotes not. that means ' ! true ' is ' false ' or ' ! false ' is ' true '
!a ---- ! true ---- false
a || b ----(true || false)----- true
a&&b ------ (true && false)----false
a||c ---------(true || true) ------- true
!(a&&b)) ------ !(true && false)-------- !(false)----true
a&&!b:----------(true && (!false))----(true && true)------true
!(!(a||c))--------------!(!(true||true))--------!(!(true))------!(false)----true
a||!(b&&c) -------(true||!(false&&true))--------(true||!(false))------(true||true)-----true
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
2)
a<b || a<10 ------------(true||true)----true
a != b&&b<10-----------(true && true)---true
a==4 || b<a ------------(false||fasle)-----false
a>4 && b<a --------------(false && false)----false
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
3) 27%3 means when '27' is divided with '3' the remainder is '0'
therefore 27%3 is '0' i.e (27%3==0) this condition becomes true.
As the if condition is true then the corresponding statements in the if block will be executed.
therefore the output is:
27 is divisible ny 3
end of sequence
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
4)
If(score==1?true:false)
{System.out.println(“you have” + score+” point.”);}
else{ System.out.println(“you have” + score+” points.”);}
this is the conditional statement.if the score is equal to 1 then if block is executed .if not equal to 1 then else block will be executed.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
5)output:
Hello3
Done
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
6)
3 ------- here case 3 will be executed so the output is ------ summer
5 -------- here 5 is not matching with any case there none of the case will be executed.In that case defalut will be executed .so the output is ---- default
1 -------- here case 1 is executed .After that there is no break starement so the next case i.e case 2 will also get executed so the ouput is ---winter summer
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
7)
Scanner sc=new Scanner(System.in);
System.out.println("Enter:");
String str=sc.next();
if(str.equals("jack") || str.equals("jill"))
{
System.out.println("Wel");
}
else
{
System.out.println("Unknown user");
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
8)
int a=10;
if(a<10); <-------------------------- semicolon means end of the statement.we dont have to mention semicolon(;) here.
{
System.out.println("a<10");
}
else
{
System.out.println("a not <10");
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
9)
int a=10;
if(a<10)
System.out.println("a<10"); <----have to put flower brackets when writing more than one line in if block or in else block
System.out.println("a is single digit");
else
{
System.out.println("a not <10");
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
10) int a=10;
If(a=10) <------------------------- have to mention double equalto (==) for comparing two variables.
{
System.out.println(“a equals to 10”);
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
11)There is no error in this piece of code.so wont get any compilation error.Executes fine.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
12)
Scanner sc=new Scanner(System.in);
System.out.println("Enter");
String str=sc.next();
if(str.endsWith(".")){
System.out.println("period");
}
else if(str.endsWith("!"))
{
System.out.println("exclamation");
}
else if(str.endsWith("?"))
{
System.out.println("question mark");
}
else
{
System.out.println("Does not end with any of these");
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
a
b
a&&b
True
true
True
true
false
False
false
true
False
false
false
false
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.