Tutor profile: Ajeet Kumar S.
Questions
Subject: Python Programming
How to input multiple values from the user in one line in Python?
For instance, in C we can do something like this: // Reads two values in one line scanf("%d %d", &x, &y) One solution is to use raw_input() two times. x, y = raw_input(), raw_input() But in Python 3, raw_input() method deprecated . And for input input() used. Another solution is to use split() x, y = raw_input().split() Note that we don’t have to explicitly specify split(‘ ‘) because split() uses any whitespace characters as delimiter as default. One thing to note in above Python code is, both x and y would be of string. We can convert them to int using another line x, y = [int(x), int(y)] # We can also use list comprehension x, y = [int(x) for x in [x, y]] Below is complete one line code to read two integer variables from standard input using split and list comprehension # Reads two numbers from input and typecasts them to int using # list comprehension x, y = [int(x) for x in raw_input().split()] Note that in Python 3, we use input() in place of raw_input().
Subject: Basic Math
1. In the first 10 overs of a cricket game, the run rate was only 3.2. What should be the run rate in the remaining 40 overs to reach the target of 282 runs?
Runs scored in the first 10 overs = 10 × 3.2 = 32 Total runs = 282 Remaining runs to be scored = 282 - 32 = 250 Remaining overs = 40 Run rate needed = 250 40 = 6.25
Subject: C++ Programming
1. What are virtual functions?
Virtual functions are used with inheritance, they are called according to the type of the object pointed or referred, not according to the type of pointer or reference. In other words, virtual functions are resolved late, at runtime. Virtual keyword is used to make a function virtual. Following things are necessary to write a C++ program with runtime polymorphism (use of virtual functions) 1) A base class and a derived class. 2) A function with the same name in base class and derived class. 3) A pointer or reference of base class type pointing or referring to an object of derived class. For example, in the following program, bp is a pointer of type Base, but a call to bp->show() calls show() function of the Derived class because bp points to an object of the Derived class. #include<iostream> using namespace std; class Base { public: virtual void show() { cout<<" In Base \n"; } }; class Derived: public Base { public: void show() { cout<<"In Derived \n"; } }; int main(void) { Base *bp = new Derived; bp->show(); // RUN-TIME POLYMORPHISM return 0; } OUTPUT = >In Derived
Contact tutor
needs and Ajeet Kumar will reply soon.