Close
Close

Practice questions on Print,print,print

Level 1

1.
Print anything you want on the screen.
print "I want to be a programmer"

2.
Store three integers in x, y and z. Print their sum.
x = 10
y = 15
z = 20
print x+y+z

3.
Store three integers in x, y and z. Print their product.
x = 5
y = 10
z = 20
print x*y*z

4.
Check type of following:
"CodesDope"
15
16.2
9.33333333333333333333
print type("CodesDope")
print type(15)
print type(16.2)
print type(9.33333333333333333333)

5.
Join two string using '+'.
E.g.-"Codes"+"Dope"
print "Codes"+"Dope"

6.
Store two values in x and y and swap them.
x = 10
y = 20
x,y = y,x
print x
print y

7.
Create a class named 'Rectangle' with two data members- length and breadth and a method to claculate the area which is 'length*breadth'. The class has three constructors which are :
1 - having no parameter - values of both length and breadth are assigned zero.
2 - having two numbers as parameters - the two numbers are assigned as length and breadth respectively.
3 - having one number as parameter - both length and breadth are assigned that number.
Now, create objects of the 'Rectangle' class having none, one and two parameters and print their areas.

8.
Suppose you have a Piggie Bank with an initial amount of $50 and you have to add some more amount to it. Create a class 'AddAmount' with a data member named 'amount' with an initial value of $50. Now make two constructors of this class as follows:
1 - without any parameter - no amount will be added to the Piggie Bank
2 - having a parameter which is the amount that will be added to Piggie Bank
Create object of the 'AddAmount' class and display the final amount in Piggie Bank.

9.
Create a class named 'Programming'. While creating an object of the class, if nothing is passed to it, then the message "I love programming languages" should be printed. If some String is passed to it, then in place of "programming languages" the name of that String variable should be printed.
For example, while creating object if we pass "Java", then "I love Java" should be printed.
class Programming{
  public Programming(String s){
    System.out.println("I love "+s);
  }
  public Programming(){
    System.out.println("I love programming languages");
  }
}

class Ans{
  public static void main(String[] args){
    Programming s = new Programming("Java");
    Programming a = new Programming();
  }
}

Level 2

Level 3

Ask Yours
Post Yours