NPTEL: Programming , Data Structures and Algorithms Using Python Week 2 Quiz answer with proof

 

Week 2 Quiz

Due date: 2022-08-10, 23:59 IST.
Your last recorded submission was on 2022-08-09, 21:54 IST
All questions carry equal weightage. All Python code is assumed to be executed using Python3. You may submit as many times as you like within the deadline. Your final submission will be graded.

Note:

  • If the question asks about a value of type string, remember to enclose your answer in single or double quotes.
  • If the question asks about a value of type list, remember to enclose your answer in square brackets and use commas to separate list items.

One of the following 10 statements generates an error. Which one? (Your answer should be a number between 1 and 10.)

x = [[3,5],"mimsy",2,"borogove",1]  # Statement 1
y = x[0:50]                          # Statement 2
z = y                                # Statement 3
w = x                                # Statement 4
x[1] = x[1][:5] + 'ery'              # Statement 5
y[1] = 4                             # Statement 6
w[1][:3] = 'fea'                     # Statement 7
z[4] = 42                            # Statement 8
x[0][0] = 5555                       # Statement 9
a = (x[3][1] == 1)                   # Statement 10
 Ans: 7



2.5 points
2.5 points

Consider the following lines of Python code.

b = [43,99,65,105,4]
a = b[2:]
d = b[1:]
c = b
d[1] = 95
b[2] = 47
c[3] = 73

Which of the following holds at the end of this code?

 
 
 
 
What is the value of endmsg after executing the following lines?
startmsg = "anaconda"
endmsg = ""
for i in range(1,1+len(startmsg)):
  endmsg = endmsg + startmsg[-i]
  Ans: "anaconda"



2.5 points
What is the value of mylist after the following lines are executed?
def mystery(l):
  l = l[2:]
  return(l)

mylist = [7,11,13,17,19,21]
mystery(mylist)
    Ans: [7,11,13,17,19,21]



2.5 points
You may submit any number of times before the due date. The final submission will be considered for grading.

netaji gandi Tuesday, August 9, 2022
Programming Data Structures And Algorithms Using Python Coding Assignment 2 Answers 2022 Week 2 Programming Assignment

 

Week 2 Programming Assignment



Write three Python functions as specified below. Paste the text for all three functions together into the submission window. Your function will be called automatically with various inputs and should return values as specified. Do not write commands to read any input or print any output.

  • You may define additional auxiliary functions as needed.
  • In all cases you may assume that the value passed to the function is of the expected type, so your function does not have to check for malformed inputs.
  • For each function, there are normally some public test cases and some (hidden) private test cases.
  • "Compile and run" will evaluate your submission against the public test cases.
  • "Submit" will evaluate your submission against the hidden private test cases. There are 10 private test cases, with equal weightage. You will get feedback about which private test cases pass or fail, though you cannot see the actual test cases.
  • Ignore warnings about "Presentation errors".

  1. Write a function intreverse(n) that takes as input a positive integer n and returns the integer obtained by reversing the digits in n.

    Here are some examples of how your function should work.

    >>> intreverse(783)
    387
    >>> intreverse(242789)
    987242
    >>> intreverse(3)
    3
    
  2. Write a function matched(s) that takes as input a string s and checks if the brackets "(" and ")" in s are matched: that is, every "(" has a matching ")" after it and every ")" has a matching "(" before it. Your function should ignore all other symbols that appear in s. Your function should return True if s has matched brackets and False if it does not.

    Here are some examples to show how your function should work.

     
    >>> matched("zb%78")
    True
    >>> matched("(7)(a")
    False
    >>> matched("a)*(?")
    False
    >>> matched("((jkl)78(A)&l(8(dd(FJI:),):)?)")
    True
    
  3. Write a function sumprimes(l) that takes as input a list of integers l and retuns the sum of all the prime numbers in l.

    Here are some examples to show how your function should work.

    >>> sumprimes([3,3,1,13])
    19
    >>> sumprimes([2,4,6,9,11])
    13
    >>> sumprimes([-3,1,6])
    
Answer Code:

def intreverse(n):
  ans = 0
  while n > 0:
    (d,n) = (n%10,n//10)
    ans = 10*ans + d
  return(ans)

def matched(s):
  nested = 0
  for i in range(0,len(s)):
    if s[i] == "(":
       nested = nested+1
    elif s[i] == ")":
       nested = nested-1
       if nested < 0:
          return(False)    
  return(nested == 0)

def factors(n):
  factorlist = []
  for i in range(1,n+1):
    if n%i == 0:
      factorlist = factorlist + [i]
  return(factorlist)

def isprime(n):
  return(factors(n) == [1,n])


def sumprimes(l):
  sum = 0
  for i in range(0,len(l)):
    if isprime(l[i]):
      sum = sum+l[i]
  return(sum)



netaji gandi
TCS Question Paper pattern

 

TCS Question Paper pattern







Get familiar with these sections and practice them with below click on TEST


Please note

  • The source of this table is TCS’s official mock link.
  • Regarding the 6th group, i.e., advanced coding, the mock link has only one question, but there have been instances where applicants were given two questions to solve in the same time duration in the actual test.

The TCS digital exam pattern may be challenging, but with ample practice and the right guidance, you can crack it. Let’s take a brief look at each section and find out what you’ll need to cover to prepare well.

1. English and Verbal Ability

This section will consist of ten questions that ought to be solved in fifteen minutes. In the mock link, the questions draw from beginner-level topics like:

  • Verbs and prepositions
  • Identifying the error in the sentence
  • Identifying the correct sequence of sentences
  • Synonyms and antonyms of difficult words, etc.

There have also been instances where the questions only involve passage comprehensions. The usual theme of the passage would be on digital technologies like:

  • Artificial intelligence
  • Machine learning
  • Deep learning, NLP
  • Cloud computing
  • Computer vision
  • Blockchain
  • Virtual reality
  • Resources for English and verbal ability section

    • All About Words by Morris Rosenblum, Maxwell Nurnberg
    • Word Power Made Easy by Norman Lewis

    2. Quantitative Aptitude

    This section requires you to solve 12 questions within 30 minutes. Hence you should focus on developing a solid grasp of concepts and the ability to think of a solution in a limited time.

    This can be attained only through practice. So you can leverage any practice material you get your hand on, some of which we’re listing below:

    Resources for quantitative aptitude section

    3. Lateral Thinking

    This section will consist of lots of brainteasers, and it entirely depends on how much laterally can you think. There’s a vast difference between thinking horizontal, vertical, or even logical – here’s how.

    The questions could involve anything—could be a rebus, finding the missing link, identifying the correct pattern—basically any brainteaser you can think of.

    The standard questions in the actual test will be pretty much the same as shown in the mock link. You’ll be given half an hour to solve seven questions. So try to attempt as many questions as possible in the time frame.

    Useful resources for lateral thinking section:

4. Agility

This section will contain two questions that need to be solved in twenty-five minutes. If you check the mock link:

  • The first question is an API-based program in JAVA
  • The second question is an exhaustive comprehension of a mathematical concept/ leading tech. They’ll ask you to analyse the passage thoroughly and draw inferences.

However, the first question can be replaced by a new language in the actual exam. Moreover, they could change the theme or topic of the comprehension. It requires you to be aware of the latest and upcoming technologies in the digital world.

Useful resource for agility section:

5. Programming Logic

Recursion, arrays, stacks, queues, linked lists, trees, binary search trees, binary heaps, graphs — CSE geeks will already be familiar with the topics of these sections. You will have 20 minutes to solve 7 questions.

To excel in this section, you must know

  • Data structures and their implementations.
  • Standard algorithms with their space and time complexities.

Useful resources for programming logic section

6. Advanced Coding

This section will be an hour-long coding round—It could consist of one or two programs to solve in the same time constraint. The difficulty level of the same will depend on the number of questions given.

If you’re given a single question, it will probably be of the same standard as CodeVita round 1 questions, otherwise, if you’re given two questions, the difficulty level will certainly decline.

Languages allowed: C, C++, Java, Python 2.7, Perl. (These languages may change over time).




netaji gandi
NPTEL - Programming In Java-Week 2: Programming Assignment 1, 2, 3, 4 and 5 Aug-2022

 



Week 2 : Programming Assignment 1



Complete the code segment to call the method  print() of class Student first and then call print() method of class School.


// This is the class named School
class School { 
    // This is a method in class School
    public void print() { 
System.out.println("Hi! I class SCHOOL."); 
    } 
// This is the class named Student
class Student { 
// This is a method in class Student
    public void print() { 
System.out.println("Hi! I am class STUDENT"); 
    } 
}

public class Question21{ 
    public static void main(String args[]){

Student s=new Student();

s.print();

School s1=new School();

s1.print();

}

}


Week 2 : Programming Assignment 2


Complete the code segment to call the method  print() of class given class Printer to print the following.

--------------------------------
Hi! I am class STUDENT
Hi! I class SCHOOL.
--------------------------------


// This is the class named Printer
class Printer { 
    // This are the methods in class Printer
    public void print() { 
System.out.println("Hi! I class SCHOOL."); 
    } 
    public void print(String s) { 
System.out.println(s); 
    } 

public class Question22{ 
    public static void main(String[] args) { 


        Printer  p=new  Printer();

        p.print("Hi! I am class STUDENT");

        p.print();

}

}


Week 2 : Programming Assignment 3


Complete the code segment to call print() method of class Question by creating a method named ‘studentMethod()’.

// This is the main class Question
public class Question23{ 
    public static void main(String[] args) { 
// Object of the main class is created
Question23 q = new Question23();
// Print method on object of Question class is called
q.studentMethod();
    }
// 'print()' method is defined in class Question
void print(Question23 object){
System.out.print("Well Done!");
}
public void studentMethod()
{
        print(this);
}

}


Week 2 : Programming Assignment 4


Complete the code segment to call default constructor first and then any other constructor in the class.

// This is the main class Question
public class Question214{
public static void main(String[] args){
Answer a = new Answer(10,"MCQ");
}
}

class Answer{
Answer(){
System.out.println("You got nothing.");
}
Answer(int marks, String type){
      this();
System.out.println("You got "+marks+" for an "+ type);
}
}

Week 2 : Programming Assignment 5


Complete the code segment to debug / complete the program which is intended to print 'NPTEL JAVA'.

public class Question215{ 
    public static void main(String[] args) {

String  nptel, space, java;

nptel= "NPTEL";
space= " ";
java= "JAVA";

System.out.print(nptel+space+java);
   }
}



netaji gandi
NPTEL Programming in Java Week 2 Assignment Answers Aug-2023

 




Week 2:Assignment 2


Due date: 2022-08-10, 23:59 IST.
Assignment not submitted
1 point
 
 
 
 Answer: c. 64
1 point
 
 
 
 Answer: b. javadoc
1 point
 
 
 
 Answer: b. npteljava
1 point
 
 
 
 Answer: b. A: jan java nptel jan java nptel

                               c. B: 11

                                        21

                                        31

1 point
 
 
 
 Answer: d. println() prints and then appends a line break.
1 point
 
 
 
 Answer: a. Greentalk
1 point
 
 
 
 Answer: c. Foo obj = new Foo();
1 point
 
 
 
 Answer: c. false
1 point
 
 
 
 Answer: c. Scanner class can be used by passing the predefined object System.in

1 point
 
 
 
 Answer: d. 17

netaji gandi

OBJECT ORIENTED PROGRAMMING USING JAVA LAB

Java Programming Lab Experiments VR-23 2025-26 Java Programming Lab Experiments Experiment 1: Fibonacci Sequence The F...