Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views7 pages

Test UserDefinedMethods Ans

The document contains a series of programming questions and tasks related to Java, focusing on concepts such as method overloading, function parameters, and GCD/LCM calculations. It includes multiple-choice questions, debugging exercises, and programming assignments that require writing Java code to demonstrate understanding of object-oriented programming principles. The document is structured into sections with varying point values, indicating a formal assessment format.

Uploaded by

schatterjee15576
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Test UserDefinedMethods Ans

The document contains a series of programming questions and tasks related to Java, focusing on concepts such as method overloading, function parameters, and GCD/LCM calculations. It includes multiple-choice questions, debugging exercises, and programming assignments that require writing Java code to demonstrate understanding of object-oriented programming principles. The document is structured into sections with varying point values, indicating a formal assessment format.

Uploaded by

schatterjee15576
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Total : 80 M SECTION A: Time : 1.

5 hours

Question 1: Choose the correct answer and write the correct option (1M * 20)
1. Parameters defined within a method signature are:
a. Local variable b. formal parameters c. Class variable d. Actual parameters
2. (A) Assertion: Method header is also referred as method prototype
(R) Reason : It signifies the identification of the method
1. Both true and R is correct explanation of A
2. Both true and R is not correct explanation of A
c. A true and R false d. A false and R True
3. A non-returnable method is represented by using
a. void b. return type other than void c. public d. method name
4. A user-defined static method calculate(int amount, double disc ) defined in a class Discount needs to be invoked from static main
method. Which of the following correctly invokes the method
a. calculate(1000); b. calculate(1000.0, 10.0);
c. Discount ob = new Discount(); ob.calculate(1000, 10.0); d. Discount.calculate(1000, 10.0);
5. When a function is defined with the return type to be boolean, the body must include _________
a. return; b. return null; c. return 1; d. return <boolean expression>
6. A function name can begin with
a. Alphabet b. digit c. sign d. space
7. An overloaded function is
1. Function with same name and different return types
2. Function with different name and different return types
3. Function with same name and different parameters
4. Function with same name and same parameters
8. A user defined function can have ______ number of parameters
a. any b. 1 c. 2 d. 4
9. What will be the output if calculate is called with amount = 1000:
public double calculate(double amount){
int total = amount - amount * 0.10;
return total;
}
a. 900 b. 900.0 c. Compile time error d. Runtime error
10. A function that modifies its parameters is called as
a) virtual function b) pure function c) impure function d) function that is called by value
11. The prototype of a function take() that returns an int and receives an array is
a) public int take(int a[]) b) public int take(int a) c) public int[] take(int a[]). d) public int[] take(int a)
12. Given the code: int change(int x, int y){
x = x* y;
return x; }
The statement to call above function will be
a) change(10, 20); b) int r = change(10, 20); c) change(10.0, 20); d) change(10);
13. An array is passed by _______ to a function
a. reference b. value c. making a call d. none of the above
14. The parameters in the method are called __________
1. Actual parameters b. Formal parameters c. void parameters d. Pure parameters
15. Static functions can access
1. static data members
2. Non static data members
3. Both static and non static data members
4. None of the above
16. A method is invoked through:
a. class b. object c. parameter d. System

17. (A) : Static keyword is used to declare variables that are unique to each instance of a class
(R): Static variables used to declare class level variables that are shared among all instances of the class

1. Both true and R is correct explanation of A


2. Both true and R is not correct explanation of A
3. A true and R false
4. A false and R True

18. Output of following code:


class Test{
public static int varone = 10;

public static void method1(){


int varone = 25;
System.out.print(varone);
}
pubic static void method2(){
System.out.print(varone);
}
public static void main(){
method1();
method2(); }
}
1. 1025 b) 1010 c) 2510 d) 2525
19. What will be the output of the following code:
void main(){
int a = 10, b = 15;
swap(a, b);
System.out.println("a = " + a + "\t" + "b = " + b);
}
void swap(int a, int b) {
a = a + b;
b = a - b;
a = a - b;
}
a. a = 10 b = 10 b. a = 15 b = 15 c. a = 10 b = 15 d. a=15 b= 10
20. Read the following passage and solve the question:
Method is a program module used at different instances in a program to perform a specific task. Method header contains access
specifier, return type a, method name and parameter list. Method signature is identification of a method and signifies it to be
invoked for a specific operation.
What comprises of method header
1. Signature, access specifier, actual parameters, void
2. Return type, access specifier, formal parameters, name
3. Signature, access specifier, actual parameters, return type
4. Formal parameters, access specifier, actual parameters, void

Question 2: (2M * 10)


1) Explain function overloading and mention the OOPS concept it implements
Polymorphism is the OOP concept implemented by overloading. Method overloading is the process of defining
functions/methods within a class, that have the same name but differ in the number and/or the data types of their arguments.
2) Debug the error in the below code
class ABC{
public static int square(int num) ; {
num * num; }
public static void main(){
int result;
square(10);
System.out.println("Square of 10 = " , result);
}
class ABC{
public static int square(int num) {
return num * num; }
public static void main(){
int result = square(10);
System.out.println("Square of 10 = " +result);
}

3) Name the following :


a. Types of functions : call by value/ call by reference
b. Keyword used to create class methods : static
c. These are declared within the () of function declaration : formal paramters
d. Keyword that specifies function returns no value : void
4) Explain the function of a return statement in Java Programming
A method returns a value through the return statement. Once a return statement is executed, the
program control moves back to the caller method skipping the remaining statements of the current
function if any. A method can have multiple return statements but only one of them will be executed.
5) Differentiate formal and actual parameters with an example

Formal parameter Actual parameter

Formal parameters appear in method definition and have Actual parameters appear in method call statement and do
datatypes not

They represent the values received by the called method. They represent the values passed to the called method

Ex : public void calc(int x){} calc(10);

6) Explain the 2 ways of passing values to a function


Call By Value Call By Reference
Actual parameters are copied to formal Formal parameters refer to actual parameters.
parameters. Any changes to formal parameters The changes to formal parameters are reflected
are not reflected onto the actual parameters. onto the actual parameters.
All primitive data types are passed using Call by All reference data types like arrays and objects
value. of classes are passed using Call by reference.
Ex: public void m1(int x){ x = x+10;)} Ex : public void m2(int a[]){ a[0] +=1;}
7) Give the prototype of the function search which receives a sentence and a word and returns 1 or 0.
public int search(String word, String sent){return 0;};
8) Differentiate pure and impure method

Pure method Impure method

Pure methods take objects and/or primitive datatypes as Impure methods change the state of received
arguments but does not modify the objects. objects.

Pure methods doesn't have side effects. Impure methods have side effects.

9) Explain the below 2 statements


public int add(int a, int b){ return a+b;}
This is the method header or method prototype that specifies the access specifier, return type,
method name and parameter list. It accepts 2 integer arguments as formal parameters and it defines
the method body.
add(10,20);
This is the method signature. It calls the method add passing actual parameters or values while calling
the method
10) State any 2 restrictions of the static method
1. Static methods cannot access other non-static members directly
2. They do not have access to “this” keyword

SECTION B ( 40 M) :
Please provide a variable listing.

Also write a main method to create object of a class and call the respective member methods in all the programs below

Q1) Write a program in java to overload the below methods:


convert(int no) : converts number by adding 1 in every digit. If digit is 9 then replace it with 0
Ex: no = 78956
Output : 89067
convert(int no, char ch) : if the no is odd then print the ascii of the character, if it is even then print the character as it is [15M]
public class OverloadConvert
{
void convert(int no) //78956
{
int t = no;
String s = "";
while(t>0) {
int d = t%10;//6
if(d==9)
s = "0" + s;//67
else
s = (d+1) + s;
t/=10;
}
System.out.println(s);
}
//OR
void convert(int no) //78956
{
int t = no;
String s = ""+no;
for(int i=0;i<s.length();i++) {
String ch =""+ s.charAt(i); // 7
int d = Integer.parseInt(ch);
if(d==9)
System.out.print(0);
else
System.out.print(d+1);
}
System.out.println();

}
//OR
void convert(int no) //78956
{
int t = no, c=0;
int newno = 0;
while(t>0) {
int d = t%10;
if(d==9) {
newno = 0 * (int)Math.pow(10,c) + newno;
}
else
{
newno = (d+1) * (int)Math.pow(10,c) + newno;
}
t/=10;
c++;
}
System.out.println(newno);
}
// OR
void convert(int no) {
int c = 0;
int t = no;
while(t>0){
c++;
t/=10;
}
c--;
int cno = 0;
while(c>=0){
int d = no / (int)Math.pow(10,c);
if(d != 9)
cno = cno *10 + (d+1);
else
cno = cno *10 ;
no = no % (int)Math.pow(10,c);
c--;
}
System.out.println(cno);
}

// OR
void convert(int no) {
int c = 0;
int t = no;
while(t>0){
c++;
t/=10;
}
c--;
while(c>=0){
int d = no / (int)Math.pow(10,c);
if(d != 9)
System.out.print(d+1);
else
System.out.print(0);
no = no % (int)Math.pow(10,c);
c--;
}
System.out.println(cno);
}

//OR
void convert(int no) {
int rev= 0;
int t = no;
while(t>0){
rev = rev * 10 * t%10;
t/=10;
}
while(rev > 0){
int d = rev % 10;;
if(d != 9)
System.out.print(d+1);
else
System.out.print(0);
rev = rev/10;
}
}
void convert(int no, char ch){
if(no%2==0)
System.out.println(ch);
else
System.out.println((int)ch);
}
}

Variable Type Purpose


c int To store count of digits
t int To temporarily store the number
cno int To store the new number
d Int To store the digit

Q2) Write a program using method name gcd(int,int) to find the GCD (Greatest Common Divisor) of two numbers passed as parameters and
return the result.
GCD of two integers is calculated by continued division method. Divide the large number by the smaller, the remainder then divides the
previous divisor. The process is repeated till the remainder is zero. The divisor then results in the GCD.
Also create a function lcm(int,int,int) that takes 2 numbers and the calculated gcd as the parameters and prints the lcm.
It calculates lcm using following formula:
LCM= (Product of two numbes)/GCD [15M]

import java.util.*;
public class Gcd_lcm
{
public int gcd(int a, int b){
int h = Math.max(a,b);//7
int l = Math.min(a,b);//5
int r = h%l;//2
while(r>0)
{
h = l;//5 2
l = r;//2 1
r = h%l;//0
}
return l;
}
public void lcm(int a,int b, int gcd){
int lcm = (a*b)/ gcd;
System.out.println("Lcm "+lcm);
}
public static void main()
{
Gcd_lcm ob = new Gcd_lcm();
int g = ob.gcd(7,5);
System.out.println("GCD "+g);
ob.lcm(7,5,g);
}
}

Q3) Write a program to enter a two digit number [ keep taking the input until it is not 2-digit ]and find out its first factor excluding 1 (one).
The program then find the second factor (when the number is divide by the first factor) and finally displays both the factors.
Hint: Use a non-return type function as void fact(int n) to accept the number and print the factors
Sample Input: 21
The first factor of 21 is 3
Sample Output: 3, 7
Sample Input: 30
The first factor of 30 is 2
Sample Output: 2, 15 [10M]

import java.util.Scanner;

public class Factors


{
public void fact(int n) {

int i;
for (i = 2; i <= n; i++) {
if (n % i == 0)
break;
}

int sf = n / i;

System.out.println(i + ", " + sf);


}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
System.out.print("Enter 2-digit number: ");
int num = in.nextInt();
while(num <10 || num > 99){
System.out.print("Enter 2-digit number: ");
num = in.nextInt();
}
Factors obj = new Factors();
obj.fact(num);
}
}

You might also like