1.

Which of the following code snippet is used to convert decimal to binary numbers?

publicvoidconvertBinary(intnum) {intbin[] =newint[50];intindex = 0;while(num > 0) { bin[index++] = num/2; num = num%2; }for(inti = index-1;i >= 0;i--) { System.out.print(bin[i]); } }

publicvoidconvertBinary(intnum) {intbin[] =newint[50];intindex = 0;while(num > 0) { bin[index++] = num%2; num = num/2; }for(inti = index-1;i >= 0;i--) { System.out.print(bin[i]); } }

publicvoidconvertBinary(intnum) {intbin[] =newint[50];intindex = 0;while(num > 0) { bin[++index] = num%2; num = num/2; }for(inti = index-1;i >= 0;i--) { System.out.print(bin[i]); } }

publicvoidconvertBinary(intnum) {intbin[] =newint[50];intindex = 0;while(num > 0) { bin[++index] = num/2; num = num%2; }for(inti = index-1;i >= 0;i--) { System.out.print(bin[i]); } }

**Answer: Option 'B'**

**Take the modulus by 2 of the number and store in an array while halving the number during each iteration and then display the contents of the array.**

2.

Write a piece of code which returns true if the string contains balanced parenthesis, false otherwise.

publicbooleanisBalanced(String exp) {intlen = exp.length(); Stack<Integer> stk =newStack<Integer>();for(inti = 0; i < len; i++) {charch = exp.charAt(i);if(ch == '(') stk.push(i);elseif(ch == ')') {if(stk.peek() ==null) {returnfalse; } stk.pop(); } }returntrue; }

publicbooleanisBalanced(String exp) {intlen = exp.length(); Stack<Integer> stk =newStack<Integer>();for(inti = 0; i < len; i++) {charch = exp.charAt(i);if(ch == '(') stk.push(i);elseif(ch == ')') {if(stk.peek() !=null) {returntrue; } stk.pop(); } }returnfalse; }

publicbooleanisBalanced(String exp) {intlen = exp.length(); Stack<Integer> stk =newStack<Integer>();for(inti = 0; i < len; i++) {charch = exp.charAt(i);if(ch == ')') stk.push(i);elseif(ch == '(') {if(stk.peek() ==null) {returnfalse; } stk.pop(); } }returntrue; }

publicbooleanisBalanced(String exp) {intlen = exp.length(); Stack<Integer> stk =newStack<Integer>();for(inti = 0; i < len; i++) {charch = exp.charAt(i);if(ch == '(') stk.push(i);elseif(ch == ')') {if(stk.peek() !=null) {returnfalse; } stk.pop(); } }returntrue; }

**Answer: Option 'A'**

**Whenever a ‘(‘ is encountered, push it into the stack, and when a ‘)’ is encountered check the top of the stack to see if there is a matching ‘(‘, if not return false, continue this till the entire string is processed and then return true.**

3.

Express -15 as a 6-bit signed binary number.

**Answer: Option 'B'**

**The first 4 1s from the right represent the number 15, 2 more bits are padded to make it 6 digits and the leftmost bit is a 1 to represent that it is -15.**

4.

What is the time complexity for converting decimal to binary numbers?

**Answer: Option 'C'**

**Since each time you are halving the number, it can be related to that of a binary search algorithm, hence the complexity is O(logn).**

5.

Using stacks, how to obtain the binary representation of the number?

publicvoidconvertBinary(intnum) { Stack<Integer> stack =newStack<Integer>();while(num != 0) {intdigit = num / 2; stack.push(digit); num = num % 2; } System.out.print("Binary representation is:");while(!(stack.isEmpty() )) { System.out.print(stack.pop()); } }

publicvoidconvertBinary(intnum) { Stack<Integer> stack =newStack<Integer>();while(num != 0) {intdigit = num % 2; stack.push(digit); } System.out.print("Binary representation is:");while(!(stack.isEmpty() )) { System.out.print(stack.pop()); } }

publicvoidconvertBinary(intnum) { Stack<Integer> stack =newStack<Integer>();while(num != 0) {intdigit = num % 2; stack.push(digit); num = num / 2; } System.out.print("Binary representation is:");while(!(stack.isEmpty() )) { System.out.print(stack.pop()); } }

**Answer: Option 'C'**

**Here instead of adding the digits to an array, you push it into a stack and while printing, pop it from the stack.**

6.

What is the time complexity of the above code?

**Answer: Option 'B'**

**All the characters in the string have to be processed, hence the complexity is O(n).**

7.

Which is the predefined method available in Java to convert decimal to binary numbers?

**Answer: Option 'D'**

**The method toBinaryString() takes an integer argument and is defined in java.lang package. Usage is java.lang.Integer.toBinaryString(int) this returns the string representation of the unsigned integer value.**

8.

For every matching parenthesis, print their indices.

publicvoiddispIndex(String exp) { Stack<Integer> stk =newStack<Integer>();for(inti = 0; i < len; i++) {charch = exp.charAt(i);if(ch == '(') stk.push(i);elseif(ch == ')') {try{intp = stk.pop() + 1; System.out.println("')' at index "+(i+1)+" matched with ')' at index "+p); }catch(Exception e) { System.out.println("')' at index "+(i+1)+" is unmatched"); } } }while(!stk.isEmpty() ) System.out.println("'(' at index "+(stk.pop() +1)+" is unmatched"); }

publicvoiddispIndex(String exp) { Stack<Integer> stk =newStack<Integer>();for(inti = 0; i < len; i++) {charch = exp.charAt(i);if(ch == '(') stk.push(i);elseif(ch == ')') {try{intp = stk.pop() + 1; System.out.println("')' at index "+(i)+" matched with ')' at index "+p); }catch(Exception e) { System.out.println("')' at index "+(i)+" is unmatched"); } } }while(!stk.isEmpty() ) System.out.println("'(' at index "+(stk.pop() +1)+" is unmatched"); }

publicvoiddispIndex(String exp) { Stack<Integer> stk =newStack<Integer>();for(inti = 0; i < len; i++) {charch = exp.charAt(i);if(ch == ')') stk.push(i);elseif(ch == '(') {try{intp = stk.pop() + 1; System.out.println("')' at index "+(i+1)+" matched with ')' at index "+p); }catch(Exception e) { System.out.println("')' at index "+(i+1)+" is unmatched"); } } }while(!stk.isEmpty() ) System.out.println("'(' at index "+(stk.pop() +1)+" is unmatched"); }

**Answer: Option 'A'**

**Whenever a ‘(‘ is encountered, push the index of that character into the stack, so that whenever a corresponding ‘)’ is encountered, you can pop and print it.
**

- Array and Array Operations | Data Structure MCQs
- Stack Operations | Data Structure MCQs
- Queue Operations | Data Structure MCQs
- Singly Linked List Operations | Data Structure MCQs
- Doubly Linked List | Data Structure MCQs
- Circular Linked List | Data Structure MCQs
- Stack using Array | Data Structure MCQs
- Stack using Linked List | Data Structure MCQs
- Queue using Array | Data Structure MCQs
- Queue using Linked List | Data Structure MCQs
- Priority Queue | Data Structure MCQs
- Double Ended Queue (Dequeue) | Data Structure MCQs
- Stack using Queues | Data Structure MCQs
- Decimal to Binary using Stacks | Data Structure MCQs
- Towers of Hanoi | Data Structure MCQs
- Bit Array | Data Structure MCQs
- Dynamic Array | Data Structure MCQs
- Parallel Array | Data Structure MCQs
- Sparse Array | Data Structure MCQs
- Matrix | Data Structure MCQs
- Skip List | Data Structure MCQs
- Xor Linked List | Data Structure MCQs
- Free List | Data Structure MCQs
- Binary Trees using Array | Data Structure MCQs
- Binary Trees using Linked Lists | Data Structure MCQs
- Preorder Traversal | Data Structure MCQs
- Inorder Traversal | Data Structure MCQs
- Binary Tree Properties | Data Structure MCQs
- Binary Search Tree | Data Structure MCQs
- AVL Tree | Data Structure MCQs

- RRB NTPC and RRB Group D Free Model Papers in Tamil Telugu Hindi and English
- Current Affairs in Telugu - RRB NTPC and RRB Group D Model Papers in Telugu
- Arithmetic and Maths in Telugu MCQs - RRB NTPC and RRB Group D Model Papers in Telugu
- Biology MCQs - RRB NTPC and RRB Group D Model Papers in Telugu
- Reasoning Practice Tests in Telugu - RRB NTPC and RRB Group D Model Papers in Telugu
- Science and Technology MCQs - RRB NTPC and RRB Group D Model Papers in Telugu
- Chemistry MCQs in Telugu - RRB NTPC and RRB Group D Model Papers in Telugu
- Physics MCQs in Telugu - RRB NTPC and RRB Group D Model Papers in Telugu
- Indian Polity - Constitution & Administration in Telugu - RRB NTPC and RRB Group D Model Papers in Telugu
- Indian Geography in Telugu MCQs - RRB NTPC and RRB Group D Model Papers in Telugu
- Andhra Pradesh - Geography in Telugu - RRB NTPC and RRB Group D Model Papers in Telugu
- General Knowledge MCQs - RRB NTPC and RRB Group D Model Papers in Telugu
- General Studies in Telugu - APPSC TSPSC UPSC - MCQs
- Andhra Pradesh Economics Question and Answers - MCQs