adplus-dvertising

Welcome to the Stacks and Queues MCQs Page

Dive deep into the fascinating world of Stacks and Queues with our comprehensive set of Multiple-Choice Questions (MCQs). This page is dedicated to exploring the fundamental concepts and intricacies of Stacks and Queues, a crucial aspect of Data Structures and Algorithms. In this section, you will encounter a diverse range of MCQs that cover various aspects of Stacks and Queues, from the basic principles to advanced topics. Each question is thoughtfully crafted to challenge your knowledge and deepen your understanding of this critical subcategory within Data Structures and Algorithms.

frame-decoration

Check out the MCQs below to embark on an enriching journey through Stacks and Queues. Test your knowledge, expand your horizons, and solidify your grasp on this vital area of Data Structures and Algorithms.

Note: Each MCQ comes with multiple answer choices. Select the most appropriate option and test your understanding of Stacks and Queues. You can click on an option to test your knowledge before viewing the solution for a MCQ. Happy learning!

Stacks and Queues MCQs | Page 24 of 25

Q231.
Select the code snippet which returns the top of the stack.
a)

public int top()
{
       if(q1.size()>0)
       {
            return q1.poll();
       }
       else if(q2.size()>0)
       {
            return q2.poll();
       }
       return 0;
}

b)

public int top()
{
       if(q1.size()==0)
       {
            return q1.peek();
       }
       else if(q2.size()==0)
       {
            return q2.peek();
       }
        return 0;
    }

c)

public int top()
{
       if(q1.size()>0)
       {
            return q1.peek();
       }
       else if(q2.size()>0)
       {
            return q2.peek();
       }
       return 0;
}

d) None of the mentioned

a.

a

b.

b

c.

c

d.

d

Discuss
Answer: (c).c
Q232.
Select the code snippet which return true if the stack is empty, false otherwise.
a)

public boolean empty()
{
     return q2.isEmpty();
}

b)

public boolean empty() 
{
     return q1.isEmpty() || q2.isEmpty();
}

c)

public boolean empty() 
{
     return q1.isEmpty();
}

d)

public boolean empty() 
{
     return q1.isEmpty() & q2.isEmpty();
}

a.

a

b.

b

c.

c

d.

d

Discuss
Answer: (d).d
Q233.
Making the pop operation costly, select the code snippet which implements the same.
a)

public int pop()
{
 int res=-999,count=0;
 if(q1.size()>0)
        {
  count = q1.size();
  while(count>0)
   q2.offer(q1.poll());
  res = q1.poll();
 }
 if(q2.size()>0)
        {
  count = q2.size();
  while(count>0)
   q1.offer(q2.poll());
  res = q2.poll();
 }
 return res;
}

b)

public int pop()
{
 int res=-999,count=0;
 if(q1.size()>0)
        {
  count = q1.size();
  while(count>1)
   q2.offer(q1.poll());
  res = q2.poll();
 }
 if(q2.size()>0)
        {
  count = q2.size();
  while(count>1)
   q1.offer(q2.poll());
  res = q1.poll();
 }
 return res;
}

c)

public int pop()
{
 int res=-999,count=0;
 if(q1.size()>0)
        {
  count = q1.size();
  while(count>1)
   q2.offer(q1.poll());
  res = q1.poll();
 }
 if(q2.size()>0)
        {
  count = q2.size();
  while(count>1)
   q1.offer(q2.poll());
  res = q2.poll();
 }
 return res;
}

d) None of the mentioned

a.

a

b.

b

c.

c

d.

d

Discuss
Answer: (c).c
Discuss
Answer: (b).Perform push() with pop as the costlier operation
Q235.
Express -15 as a 6-bit signed binary number.
Discuss
Answer: (b).101111
Q236.
Which of the following code snippet is used to convert decimal to binary numbers?
a)

public void convertBinary(int num)
{
     int bin[] = new int[50];
     int index = 0;
     while(num > 0)
     {
       bin[index++] = num%2;
       num = num/2;
     }
     for(int i = index-1;i >= 0;i--)
     {
       System.out.print(bin[i]);
     }
}

b)

public void convertBinary(int num)
{
     int bin[] = new int[50];
     int index = 0;
     while(num > 0)
     {
       bin[++index] = num%2;
       num = num/2;
     }
     for(int i = index-1;i >= 0;i--)
     {
       System.out.print(bin[i]);
     }
}

c)

public void convertBinary(int num)
{
     int bin[] = new int[50];
     int index = 0;
     while(num > 0)
     {
         bin[index++] = num/2;
         num = num%2;
     }
     for(int i = index-1;i >= 0;i--)
     {
         System.out.print(bin[i]);
     }
}

d)

public void convertBinary(int num)
 {
     int bin[] = new int[50];
     int index = 0;
     while(num > 0)
     {
         bin[++index] = num/2;
         num = num%2;
     }
     for(int i = index-1;i >= 0;i--)
     {
         System.out.print(bin[i]);
     }
  }

a.

a

b.

b

c.

c

d.

d

Discuss
Answer: (a).a
Q237.
Which is the predefined method available in Java to convert decimal to binary numbers?
a)

public void convertBinary(int num)
{
    Stack<Integer> stack = new Stack<Integer>();
    while (num != 0)
    {
        int digit = num / 2;
        stack.push(digit);
        num = num % 2;
    } 
    System.out.print("\nBinary representation is:");
    while (!(stack.isEmpty() ))
    {
        System.out.print(stack.pop());
    }
 }

b)

public void convertBinary(int num)
{
    Stack<Integer> stack = new Stack<Integer>();
    while (num != 0)
    {
        int digit = num % 2;
        stack.push(digit);
    } 
    System.out.print("\nBinary representation is:");
    while (!(stack.isEmpty() ))
    {
        System.out.print(stack.pop());
    }
 }

c)

public void convertBinary(int num)
{
    Stack<Integer> stack = new Stack<Integer>();
    while (num != 0)
    {
        int digit = num % 2;
        stack.push(digit);
        num = num / 2;
    } 
    System.out.print("\nBinary representation is:");
    while (!(stack.isEmpty() ))
    {
        System.out.print(stack.pop());
    }
 }

d) None of the mentioned
Discuss
Answer: (d).toBinaryString(int)
Q238.
Using stacks, how to obtain the binary representation of the number?
a)

public void convertBinary(int num)
{
    Stack<Integer> stack = new Stack<Integer>();
    while (num != 0)
    {
        int digit = num / 2;
        stack.push(digit);
        num = num % 2;
    } 
    System.out.print("\nBinary representation is:");
    while (!(stack.isEmpty() ))
    {
        System.out.print(stack.pop());
    }
 }

b)

public void convertBinary(int num)
{
    Stack<Integer> stack = new Stack<Integer>();
    while (num != 0)
    {
        int digit = num % 2;
        stack.push(digit);
    } 
    System.out.print("\nBinary representation is:");
    while (!(stack.isEmpty() ))
    {
        System.out.print(stack.pop());
    }
 }

c)

public void convertBinary(int num)
{
    Stack<Integer> stack = new Stack<Integer>();
    while (num != 0)
    {
        int digit = num % 2;
        stack.push(digit);
        num = num / 2;
    } 
    System.out.print("\nBinary representation is:");
    while (!(stack.isEmpty() ))
    {
        System.out.print(stack.pop());
    }
 }

d) None of the mentioned

a.

a

b.

b

c.

c

d.

d

Discuss
Answer: (c).c
Q239.
What is the time complexity for converting decimal to binary numbers?
Discuss
Answer: (c).O(logn)
Q240.
Write a piece of code which returns true if the string contains balanced parenthesis, false otherwise.
a)

public boolean isBalanced(String exp)
{
 int len = exp.length();
 Stack<Integer> stk = new Stack<Integer>();
 for(int i = 0; i < len; i++)
        {
  char ch = exp.charAt(i);
                if (ch == '(')
                stk.push(i);
                else if (ch == ')')
                {
   if(stk.peek() == null)
                        {
    return false;
   }
   stk.pop();
  }
 }
 return true;
}

b)

public boolean isBalanced(String exp)
{
 int len = exp.length();
 Stack<Integer> stk = new Stack<Integer>();
 for(int i = 0; i < len; i++)
            {
  char ch = exp.charAt(i);
                if (ch == '(')
                stk.push(i);
                else if (ch == ')')
                {
   if(stk.peek() != null)
                        {
    return true;
   }
   stk.pop();
  }
 }
 return false;
  }

c)

public boolean isBalanced(String exp)
{
 int len = exp.length();
 Stack<Integer> stk = new Stack<Integer>();
 for(int i = 0; i < len; i++)
        {
        char ch = exp.charAt(i);
               if (ch == ')')
               stk.push(i);
               else if (ch == '(')
               {
   if(stk.peek() == null)
                        {
    return false;
   }
   stk.pop();
  }
 }
 return true;
}

d)

public boolean isBalanced(String exp)
{
 int len = exp.length();
 Stack<Integer> stk = new Stack<Integer>();
 for(int i = 0; i < len; i++)
        {
  char ch = exp.charAt(i);
                if (ch == '(')
                stk.push(i);
                else if (ch == ')')
                {
   if(stk.peek() != null)
                        {
    return false;
   }
   stk.pop();
  }
 }
 return true;
  }

a.

a

b.

b

c.

c

d.

d

Discuss
Answer: (a).a

Suggested Topics

Are you eager to expand your knowledge beyond Data Structures and Algorithms? We've curated a selection of related categories that you might find intriguing.

Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!