Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Q)Coding Question You are given a binary string(i

Q)Coding Question You are given a binary string(i

Computer Science

Q)Coding Question You are given a binary string(i.e. with characters 0 and 1) S consisting of characters S1, S2, ..., SN. In a single operation, you can choose two indices L and R such that 1 ≤ L ≤ R ≤ N and flip the characters SL, SL+1, ..., SR. By flipping, we mean change character 0 to 1 and vice-versa.

Your aim is to perform ATMOST one operation such that in final string number of 1s is maximised. If you don't want to perform the operation, return an empty array. Else, return an array consisting of two elements denoting L and R. If there are multiple solutions, return the lexicographically smallest pair of L and R.

Notes:

Pair (a, b) is lexicographically smaller than pair (c, d) if a < c or, if a == c and b < d.

For example,

S = 010

Pair of [L, R] | Final string

_______________|_____________

[1 1]     | 110

[1 2]     | 100

[1 3]     | 101

[2 2]     | 000

[2 3]     | 001

We see that two pairs [1, 1] and [1, 3] give same number of 1s in final string. So, we return [1, 1].

Another example,

If S = 111

No operation can give us more than three 1s in final string. So, we return empty array [].

 

Code will be tested with large test case files please note it, Please don't copy. MCQs with proper explanation needed

 

1. What will be the output of the following Python code?

def power(x, y=2):

  r = 1

  for i in range(y):

    r = r * x

  return r

print power(3)

print power(3, 3)

a)

212

32

b)

9

27

c)

567

98

d) None of the mentioned

 

2. The output of the following two Python codes are the same.

p = re.compile('hello')

r = p.match('hello everyone')

print(r.group(0))

r = re.match('hello', 'hello everyone')

print(r.group(0))

a) True

b) False

 

3. What is the type of sys.argv?

a) set

b) list

c) tuple

d) string

 

4. What will be the output of the following Python code?

sentence = 'horses are fast'

regex = re.compile('(?P<animal>w+) (?P<verb>w+) (?P<adjective>w+)')

matched = re.search(regex, sentence)

print(matched.groupdict())

a) {'animal': 'horses', 'verb': 'are', 'adjective': 'fast'}

b) ('horses', 'are', 'fast')

c) 'horses are fast'

d) 'are'

 

5. State whether true or false.

s = time.time()

t= time.time()

s == t

a) True

b) False

 

6. Which of the following refers to mathematical function?

a) sqrt

b) rhombus

c) add

d) rhombus

 

7. What will be the output of the following Python expression?

round(4.576)

a) 4.5

b) 5

c) 4

d) 4.6

 

8. What is delattr(obj,name) used for?

a) To print deleted attribute

b) To delete an attribute

c) To check if an attribute is deleted or not

d) To set an attribute

 

9. What will be the output of the following Python code?

class Test:

  def init(self):

    self.x = 0

class Derived_Test(Test):

  def init(self):

    Test.init(self)

    self.y = 1

def main():

  b = Derived_Test()

  print(b.x,b.y)

main()

a) Error because class B inherits A but variable x isn't inherited

b) 0 0

c) 0 1

d) Error, the syntax of the invoking method is wrong

 

10. What will be the output of the following Python function?

import math

abs(math.sqrt(25))

a) Error

b) -5

c) 5

d) 5.0

 

11. What will be the output of the following Python code?

class objects:

  def init(self):

    self.colour = None

    self._shape = "Circle" 

  def display(self, s):

    self._shape = s

obj=objects()

print(obj._objects_shape)

a) The program runs fine because name mangling has been properly implemented

b) Error because the member shape is a protected member

c) Error because the proper syntax for name mangling hasn't been implemented

d) Error because the member shape is a private member

 

12. If getstate() returns _______________ the setstate() module will not be called on pickling.

a) True value

b) False value

c) ValueError

d) OverflowError

 

13. What will be the output of the following Python code snippet?

total={}

def insert(items):

  if items in total:

    total[items] += 1

  else:

    total[items] = 1

insert('Apple')

insert('Ball')

insert('Apple')

print (len(total))

a) 3

b) 1

c) 2

d) 0

 

14. Where are the arguments received from the command line stored?

a) sys.argv

b) os.argv

c) argv

d) none of the mentioned

 

15. What will be the output of the following Python code?

def a(b):

  b = b + [5]

c = [1, 2, 3, 4]

a(c)

print(len(c))

a) 4

b) 5

c) 1

d) An exception is thrown

 

16. What will be the output of the following Python code?

def a(n):

  if n == 0:

    return 0

  elif n == 1:

    return 1

  else:

    return a(n-1)+a(n-2)

for i in range(0,4):

  print(a(i),end=" ")

a) 0 1 2 3

b) An exception is thrown

c) 0 1 1 2 3

d) 0 1 1 2

 

17. What will be the output of the following Python code?

def writer():

 title = 'Sir'

 name = (lambda x:title + ' ' + x)

 return name

who = writer()

who('Arthur')

a) Arthur Sir

b) Sir Arthur

c) Arthur

d) None of the mentioned

 

18. Which of the following statements isn't true?

a) A non-private method in a superclass can be overridden

b) A derived class is a subset of superclass

c) The value of a private variable in the superclass can be changed in the subclass

d) When invoking the constructor from a subclass, the constructor of superclass is automatically invoked

 

19. What will be the output of the following Python code?

x=1

def cg():

 global x

 x=x+1  

cg()

x

a) 2

b) 1

c) 0

d) Error

 

20. Which one of the following is not attributes of file?

a) closed

b) softspace

c) rename

d) mode

 

21. What will be the output of the following Python code?

def foo():

  return total + 1

total = 0

print(foo())

a) 0

b) 1

c) error

d) none of the mentioned

 

22. What will be the output of the following Python code?

def f1():

  global x

  x+=1

  print(x)

x=12

print("x")

a) Error

b) 13

c)

13

x

d) x

 

23. Which function overloads the + operator?

a) add()

b) plus()

c) sum()

d) none of the mentioned

 

24. What will be the output of the following Python code?

class Demo:

  def check(self):

    return " Demo's check "  

  def display(self):

    print(self.check())

class Demo_Derived(Demo):

  def check(self):

    return " Derived's check "

Demo().display()

Demo_Derived().display()

a) Demo's check Derived's check

b) Demo's check Demo's check

c) Derived's check Demo's check

d) Syntax error

 

25. Which of the following is not a class method?

a) Non-static

b) Static

c) Bounded

d) Unbounded

 

26. What will be the output of the following Python code?

class A:

  def test1(self):

    print(" test of A called ")

class B(A):

  def test(self):

    print(" test of B called ")

class C(A):

  def test(self):

    print(" test of C called ")

class D(B,C):

  def test2(self):

    print(" test of D called ")     

obj=D()

obj.test()

a)

test of B called

test of C called

b)

test of C called

test of B called

c) test of B called

d) Error, both the classes from which D derives has same method test()

 

27. Which function is used to write all the characters?

a) write()

b) writecharacters()

c) writeall()

d) writechar()

 

28. What is the value of x if x = math.factorial(0)?

a) 0

b) 1

c) error

d) none of the mentioned

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE