## Part I [20 Marks]

Answer all questions in PART I (compulsory) and six questions from PART II, choosing two questions from Section A, two questions from Section B and two questions from Section C.

**Q1. a) Draw the truth table to prove 1st commutative law.**

p q p + q q + p

0 0 0 0

0 1 1 1

1 0 1 1

1 1 1 1

**b) Differentiate between ASCII code and Unicode.**

The ASCII code is a 7-bit code.

Unicode is a 16-bit code.

**c) Minimize F = (AB)’ + AB’C + AC using Boolean laws.**

(AB)’ + AB’C + AC

= A’ + B’ + AB’C + AC

= A’ + B’ + AC(B’ + 1)

= A’ + B’ + AC.

**d) Write an equivalent Java expression for (2x ^{5}y^{3} – 4×2) / (√xy).**

(2 * Math.pow(x, 5) * Math.pow(y, 3) – 4 * x * x) / (Math.sqrt(x * y))

**e) Can a static class member of public class be accessed by class in any other package?**

No.

**Q2. a) What is wff? Give one example.**

WFF (Well Formed Formula) is another term for proposition.

Example: It is raining.

**b) Differentiate between break and continue. Give one example for each to illustrate the difference.**

The break keyword is used to exit from a switch statement or a loop.

for(i = 1; i <= 10; i++){

if(i % 3 == 0)

break;

System.out.println(i);

}

OUTPUT:

1

2

The continue keyword is used to take the program flow to the next iteration of the loop.

for(i = 1; i <= 10; i++){

if(i % 3 == 0)

continue;

System.out.println(i);

}

OUTPUT:

1

2

4

5

7

8

10

**c) State why some gates are called universal gates? Give an example.**

Some gates are called universal gates because other switching functions (AND, OR, NOT) can easily be implemented using these gates.

Example: NOR gate or NAND gate.

**d) Find the equivalent expression for (p ↔ q) + (p → q).**

(p ↔ q) + (p → q)

= (p → q)(q → p) + (p → q)

= (p → q)(q → p + 1)

= (p → q)

**e) If A denotes “Today is Saturday” and B denotes “Saturday is fun day”, then express the following statements in symbolic form:****i) Today is not Saturday and Saturday is a fun day.**

A’B**ii) If and only if today is Saturday then it is a fun day.**

A ↔ B

**Q3. Find the output for the following function when the value of x = 97.****Show the dry run/working.**

void work(int x){char ch;do{ch = (char)x;System.out.println(ch + "\t");if(x % 10 == 0)break;x++;}while(x <= 126);}

x ch

97 a

98 b

99 c

100 dOUTPUT:

a

b

c

d

## Part II

Answer six questions in this part, choosing two questions from Section A, two questions from Section B and two from Section C.

## Section A [20 Marks]

**Q4. Perform the following:****a) (101101.0101) _{2} = (?)_{10}**

1 × 2

^{5}+ 1 × 2

^{3}+ 1 × 2

^{2}+ 1 × 2

^{0}+ 1 × 2

^{-2}+ 1 × 2

^{-4}

= 32 + 8 + 4 + 1 + 1/4 + 1/16

= 45 + 0.25 + 0.0625

= 45.3125

_{10}

**b) (1110101.111110) _{2} = (?)16**

75.F8

_{16}

**c) (375) _{8} = (?)_{16}**

FD

_{16}

**d) (1101) _{2} – (111)_{2} using 2’s complement method.**

= 1101 – 0111

= 1101 + (-0111)

2’s complement of 0111 = 1001.

= 1101 + 1001

= 10110

Extra bit is truncated to get 0110.

**e) (101011) _{2} – (111)_{2}**

101011

- 111

------

100100

------

**Q5. a) A strong box needs 2 keys for opening it. One key is always with the Manager (M), one key is with the Deputy Manager and one key is with the supervisor.****M denotes Manager’s key.****D denotes Deputy Manager’s key.****S denotes Supervisor’s key.****F denotes the status of the strong box. 1 means open, and 0 means locked.****1 means key is present.****0 means key is not present.****Draw a truth table to illustrate the possible combinations when the strong box can be opened.****Write the Boolean expression that will represent each truth value in the output column with conjunctive operators and join the terms using disjunctive operators.**

M D S F Conjunction

0 0 0 0

0 0 1 0

0 1 0 0

0 1 1 0

1 0 0 0

1 0 1 1 MD'S

1 1 0 1 MDS'

1 1 1 1 MDS

MD'S + MDS' + MDS

**b) Define giving one example for each:****i) Contingency:** Propositions that have some combination of 1s and 0s in their truth table column. Example: p → q**ii) Converse:** Conditional proposition determined by interchanging antecedent and consequent of given conditional. Example: Converse of p → q is q → p.**iii) Contrapositive:** Contrapositive of a conditional is formed by creating another conditional that takes its antecedent as negative consequent of earlier conditional and consequent as negated antecedent of earlier conditional. Example: Contrapositive of p → q is ~q → ~p.

**Q6. a) Draw the truth table for a 3-input XOR gate.**

A B C A ⊕ B ⊕ C

0 0 0 0

0 0 1 1

0 1 0 1

0 1 1 0

1 0 0 1

1 0 1 0

1 1 0 0

1 1 1 1

**b) Determine whether the following is contingency or contradictory:****(p ∨ q) → (p ^ q)**

p q p + q pq (p + q) → pq

0 0 0 0 1

0 1 1 0 0

1 0 1 0 0

1 1 1 1 1It is a contingency.

**c) Draw the truth table for a full adder and its logic circuit diagram.**

X Y Z SUM CARRY

0 0 0 0 0

0 0 1 1 0

0 1 0 1 0

0 1 1 0 1

1 0 0 1 0

1 0 1 0 1

1 1 0 0 1

1 1 1 1 1

**d) Draw the logic circuit diagram for (X’Y)(X + Y’) using NOR gates only. Only X and Y are available as inputs.**

## Section B [20 Marks]

**Q7. Write a program in Java to accept a sentence which is terminated by either ‘.’, ‘?’ or ‘!’. Each word of the sentence is separated by single space. Decode the words according to their potential and arrange them in ascending order.****The potential of a word is formed by adding the encrypted value of the alphabet.****For example:****KITE****Potential = 11 + 9 + 20 + 5 = 45.****A = 1, B = 2, C = 3, … Z = 26.**

```
import java.io.*;
class Potential{
public static void main(String args[])
throws IOException{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.print("Sentence: ");
String s = br.readLine();
s = s.trim();
char ch = s.charAt(s.length() - 1);
if(ch != '.' && ch != '?' && ch != '!'){
System.out.println("Invalid sentence!");
return;
}
int count = 0;
int i = 0;
int j = 0;
for(i = 0; i < s.length(); i++){
ch = s.charAt(i);
switch(ch){
case ' ':
case '.':
case '?':
case '!':
count++;
}
}
String w[] = new String[count];
int p[] = new int[count];
String word = "";
for(i = 0; i < s.length(); i++){
ch = s.charAt(i);
switch(ch){
case ' ':
case '.':
case '?':
case '!':
w[j] = word;
p[j] = potential(word);
j++;
word = "";
break;
default:
word += ch;
}
}
for(i = 0; i < p.length; i++){
for(j = 0; j < p.length - 1 - i; j++){
if(p[j] > p[j + 1]){
int temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
String t = w[j];
w[j] = w[j + 1];
w[j + 1] = t;
}
}
}
System.out.println("\nSorted list:");
for(i = 0; i < p.length; i++){
System.out.println(w[i] + "\t" + p[i]);
}
}
public static int potential(String word){
int p = 0;
word = word.toUpperCase();
for(int i = 0; i < word.length(); i++){
char ch = word.charAt(i);
p += (ch - 64);
}
return p;
}
}
```

**Q8. Write a program in Java to input a number and check whether it is a pronic number or heteromecic number.****A pronic number/oblong number/rectangular number/heteromecic number is a number which is the product of two consecutive integers, i.e. N(N + 1).****The first few pronic numbers are 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132. 156, 182, 210, 240, 272, 306, 380, 420, 462…**

```
import java.io.*;
class Pronic{
public static void main(String args[])
throws IOException{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.print("N = ");
int n = Integer.parseInt(br.readLine());
int p = 0;
while(p * (p + 1) < n)
p++;
if(p * (p + 1) == n)
System.out.println(n + " is Pronic.");
else
System.out.println(n + " is not Pronic.");
}
}
```

**Q9. Write a program in Java to input a word and print its anagrams. Anagrams are the words that are made up of all the characters present in the original word by rearranging the characters.****For example:****INPUT:****TOP****OUTPUT:****TOP, TPO, OTP, OPT, PTO, POT**

```
import java.io.*;
class Anagrams{
public static void generate(String s, String w){
if(w.length() <= 1)
System.out.println(s+w);
else{
for(int i = 0; i < w.length(); i++){
String a = w.substring(i, i + 1);
String b = w.substring(0, i);
String c = w.substring(i + 1);
generate(s + a, b + c);
}
}
}
public static void main(String args[])
throws Exception{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.print("Word: ");
String word = br.readLine();
word = word.trim();
int i = word.indexOf(' ');
if(i > 0)
word = word.substring(0, i);
word = word.toUpperCase();
System.out.println("\nAll possible anagrams:");
generate("", word);
}
}
```

## Section C [10 marks]

**Q10. Write a recursive function void fibo(int, int, int) for printing Fibonacci series up to N terms.**

```
//a = -1, b = 1, n = num. of terms
public static void fibo(int a, int b, int n){
int sum;
if(n > 0){
sum = a + b;
System.out.print(sum + "\t");
a = b;
b = sum;
fibo(a, b, n - 1);
}
}
```

**Q11. Write a function void bubbleSort(String names[]), which receives an array of names and arrange the names in alphabetical order by using bubble sort technique.**

```
public static void bubble(String a[]){
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a.length - 1 - i; j++){
if(a[j].compareToIgnoreCase(a[j + 1]) > 0){
String temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
```

**Q12. Write a function String vowel(String w). that receives a word, removes all the vowels in it and finally returns the new word.**

```
public static String vowel(String w){
String t = "";
for(int i = 0; i < w.length(); i++){
char ch = w.charAt(i);
ch = Character.toUpperCase(ch);
switch(ch){
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
continue;
default:
t += ch;
}
}
return t;
}
```

Sir,a program is given which states that:

Write a program in Java to accept a sentence which is terminated by either ‘.’, ‘?’ or ‘!’. Each word of the sentence is separated by single space. Decode the words according to their potential and arrange them in ascending order.

The potential of a word is formed by adding the encrypted value of the alphabet.

For example:

KITE

Potential = 11 + 9 + 20 + 5 = 45.

A = 1, B = 2, C = 3, … Z = 26.

Sir how to do this program using StringTokenizer?

See the second program in this post.