You are on page 1of 5

MOCK SET-1 FUNCTIONS ,CONSTRUCTORS & ARRAYS FM:-40

Q.1.Choose the correct answer:-(1X20=20M)


a) The keyword which specifies that a method does not return a value is-(return,void,public,static)
b)A method with the same name as of the class and with arguments and no return data type is termed as-(parameterized constructor,default
constructor,non-parameterised constructor,wrapper class method)
c)Assertion(A)-In POP, emphasis is laid on the functions or procedures.
Reason(R)-The functions share global data.( Both (A) and (R) are true and (R) is a correct explanation of (A),Both (A) and (R) are true and (R)is
not correct explanation of (A),(A) is true and (R)is false,(A) is false and (R) is true)
d)A ________ method needs to be called with help of an object.(void,class,non-static,static)
e)Parameters used in the method call statement are-(actual parameters,informal parameters,formal parameters,void parameters)
f)The concept of having more than one constructor with different types of parameters is called-(copy constructor,method
overloading,constructor overloading,overloading methods)
g)Variable that is declared with in the body of a method is termed as-(instance variable,class variable,local variable,argument variable)
h)A variable bounded to the object itself is called as-(instance variable,class variable,local variable,argument variable)
i)The access modifier which gives most accessibility to methods is-(public,private,protected,package)
j)The statement to create an object of a class Phone is:(Phone ob=create object();,Phone ob=create phone();,Phone ob=new Phone();,Phone
obj=new phone;
k)What will the value of p result in : int arr[]={12,5,8,10,11,4}; int p=arr.length;(12,6,4,0);
l)Which of the following statements is appropriate for a constructor?(it is always public,it never returns a value,it has the same name as the
class,all of them)
m)Which of the following function does not return a value?(pure method,impure method,mixed method,new method)
n)int m[]={9,12,10,15,14};int a[0]=11;int p=a[0]+m[1];System.out.println(p);(20,22,23,24};
o)The number of bytes occupied by a character array of 10 elements-(20 bytes,60 bytes,40 bytes,120 bytes)
p)Java statement to access the 5th element of an array is:-(x[4],x[5],x[3],x[0])
q)Constructor overloading follows which principle of OOP?(inheritance,polymorphism,abstraction,encapsulation)
r) class Output
{ public static int sum(int x)
{ return x; }
static void main()
{ sum(10); sum(10,20); sum(10,20,30); sum(10,20,30,40); }}
Which will invoke the sum()-
(only sum(10), only sum(10,20), only sum(10) & sum(10,20), all of the mentioned)
s) Method which is part of the class rather than an instance of a class is termed as-(static method,non-static method,wrapper class,string
method)
t)employee ob=new employee(); instantiation is done by -(employee,employee(),new,ob)
Q.2.Do as directed:-(2X10=20M)
a)Consider the following program and answer the questions given below:-
class Sample{int a,b;Sample(int x,int y){a=x;b=y;}void calculate(){int z;z=a+b;System.out.println(z);}
(i)Name the global variables a,b (ii)Name the method variables x,y
b)String x[]={“Artificial Inteligence”,”IOT”,”Machine learning”,”Big data”};
Give the output of the following statements:-
i)System.out.println(x[3]); Big data ii)System.out.println(x.length); 4
c)Write a prototype to declare a function equal which takes in two strings as parameter and returns the one which has more number of
characters.
String equal(String s,String s1)
{if(s.length()>s1.length())
return s.length();
else
return s1.length();
d)Given two arrays a[]={12,14,15} and b[]={45,56}; need to be merged into a single array c. i)What will be the size of array c ii)Write the first
and last index of array c. i)5 ii)a[0]=12 a[4]=56
e)What will be the output if x=0 and y=0
class test{public static void main(int x,int y)
{for(int z=0;z<4;z++)
{if((++x>2)&&(++y>2))
{++x;}
System.out.println(x);}}
1
2
3
4
f) What will be the output if a=10,b=0
class opp{public static void fun(int a,int b)
{ifa>10&&a/b==10)System.out.println(“Inside the If condition”);System.out.println(“Outside the if condition”);} Outside the if condition
g) int a[][]={{0,1,2},{7,8,9}} what will be the value of a[0][0] and a[1][2] i)0 ii)9
h) Given the array int x[]={11,22,33,44}; the value of x[1+2] 44
i) Write single line java statement to create an object sc1 if class SpecialCode, using the constructor which takes in 3 int parameters
SpecialCode sc1=new SpecialCode(4,5,6);
j) Given integer array na, write the statement which would display the sum of first and last elements of the array.
System.out.println(na[0]+na[na.length-1]);
********************************************************************************************************************
MOCK SET-2 FUNCTIONS ,CONSTRUCTORS & ARRAYS FM:-40
Q.1.Choose the correct answer:-(1X20=20M)
a) Which of the following return statement is wrong? (return 0,return (s);,return(s,p),return(4);)
b) What makes the two or more functions having the same name different?(number of arguments,type of arguments,return type,all of
the above)
c)Which of the following is a correct prototype of a function check which receives a character ch and integer n and returns true or
false?(public int check(char ch,int n),public boolean chk(char ch,int n),public boolean check(char ch,integer n),public boolean
check(char ch,int n))
d)Which type of the function increases the value of its return type, each time the function is called-(non-static function,dtatic
function,impure function,none of the above)
e)The keyword which refers to the current object is(new,private,void,this)
f)The second last element of an array of size 100,has index:-(97,99,98,100)
g)Given an array ar[]={10,20,30,40,50}; the statement ar[3]-1 will return-(40,30,39,29)
h)Name the search algorithm which compares the sought key value with the key value of middle element of the array at each stage.
(bubble sort,linear search,binary search,selection sort)
i)Which of the following statements contains an error?(int a[]=int[10],float d[]={1,2,3};int a[]={1,2};int b[];b=a;,all of the above)
j)How many elements will be stored in a 2 D array of size 5X3-(8,20,15,35)
k)which of the following is correct way to initialize a 3X2 two-dimensional array?(int dim[][]={{1,2,3},{8,7,6}};,int dim[][]=new
int{{1,2,3},{8,7,7}};,int dim{}[]={{1,2,3,8,7,6}};,int dim[][]={{1,2},{5,4},{8,7}};
l)A function which returns a value to its caller module. It also doesn’t change the states of an object.(impure
method,mutator,pure,accessor)
m)A constructor that is used to initialize the instance variables of an object by copying the initial values of the instance variables from
another object.(non-parameterised,parameterized,constructor,copy constructor)
n)Constructors doesn’t need to be called from main method-(true/false)
o)Choose the odd one out-(return type,local variables,parameter list,access specifier)
p)Invoking a function means-(calling a function,defining a function,creating a function,none)
q)Constructors are both user defined and compiler created-(true/false)
r)Choose the odd one-(actual parameter,formal parameters,class variables,constants)
s)Method overloading is one of the ways that java supports-(encapsulation,class,inheritance,polymorphism)
t)Methods can be overloaded with a difference only in th type of the return value.(true/false)
Q.2.Do as directed :-(2X10=20M)
a)If int n[]={1,2,3,4,5,6,7,9,13,16}; what are the values of x and y, if x=Math.pow(n[4],n[2]); y=Math.sqrt(n[6]+n[7]);
125.0 4.0
b)Give the output of the following code: int a[]={2,4,6,8};
for(i=0;i<=1;i++){
s=a[i]+a[3];
System.out.println(s);}
10
12
c) What are the two ways of invoking functions?i)call by value ii)call by reference
d)int change(int p,int q){p=p*q;return p+q;} what is the output of the above function if called- change(10,11)? 121
e)class B{int b;B(){b=25+35;}void display(){System.out.println(b);} what is the output 60
f) class abc{int a,int b;
void print(){System.out.println(“a=”+b); System.out.println(“b=”+a); when the function is called with values a=15,b=16 what is the
output? a=16 b=15
g) write output:-
int a[]=new int[6];
for(int i=0;i<6;i++)
{if(i%2==0)a[i]=i;
else if(a[i]%3==0)
a[i]=i*i;
else a[i]=i-I;
System.out.print(a[i]+” “);} 0129425
h) static int fn(int a,int b)
{if(a>10 || b>10)return a+b;
else if(a>b)return a-b;
else return a*b; when i)fn(10,20 ii)fn(20,20) i)30 ii)40
i)Write the prototype of a function check, which takes two integer parameters and returns the largest value using ternary operator.
int check(int a,int b){return(a>b)?a:b;}
j) Given, ar[]={10,22,33,44,55,66,77};
Write:- i)no.of elements of array ii)Range of indices i)7 ii)0 - 6
MOCK SET-3 FUNCTIONS ,CONSTRUCTORS & ARRAYS FM:-40
Q.1.Choose the correct answer:-(1X20=20M)
a) All Java methods must have a return type. (TRUE / FALSE)
b) A constructor is a special member function that is called automatically when an/a _________ of a class is declared.
(property,state,behaviour,object)
c)This operator used inside java method refers to_____ variable.(instance,static,class,local)
d) A ____________ function does not change the state of the object.(accessor,mutator,impure,predefined)
e) Which one is true about a constructor ?( constructor must have the same name as the class it is declared within, constructor is used to
create objects, constructor may be declared private,all of the above)
f) A constructor is a special ____________ of a class.(datatype,method,instantiation,object)
g)If int n[]={16,4,2,8,1};x=Math.pow(n[3],n[2]);(16.0,6.0,8.0,64.0)
h)Invoking a method by passing the objects of a class is termed as:(call by value,call by reference,call by object,call by instance)
i)In which case will the bubble sort terminate in only one iteration.(if the array is already sorted the required way,if the array is sorted in the
opposite way,if the array is constant, i.e.,all elements are same,both a and c)
j)A variable bounded to the object itself is called as-(instance,class,local,argument)
k) What will be the result when the following statement is executed? int count=new Integer(10);[variable count will be initialized to value
10,variable count will be initialized to with default value of int,An array count will be initialized with 10 element all having default value 0,Value
of count will be unknown as no value has been assigned yet]
l) class Main1 { public void main()
{ System.out.println(fun()); } double fun() {
return 20; } } [20,20.0,Compiling error,Runtime error]
m) int change(int p, int q)
{ p = p * q; return p;} What is the output of the above function if called change(10, 11) ? [10,11,110,120]
n) Given an array char a[]={‘1’,’2’,’3’}; How many bytes will it occupy[12,6,3,24]
o) What is required for binary search?(The array must be sorted in ascending order ,The array must be sorted either in ascending or
descending order,The length of the array must be known,Both (b) and (c))
p) Identify the correct array creation statement()int a[10],int a[]=new int[10];int arr[i]=10;int a[10]=new int[];
q) What is the value of c after following code is executed?
int a[]={2,4,6,8,10};a[0]=23;a[3]=a[1];int c=a[0]+a[1];(27,6,10,29)
r)The constructors that accept arguments are called:(default,actual,parameterised,static)
s)Parameters used in method call statement are known as-(formal,actual,calling,passed)
t)Which of the following types can be used for a non-returnable method?(int,float,double,void)
Q.2.Do as directed:-(2X10=20M)
a)A matrix of 3X3 is given as 456
123
789
Write statement to store these values in an array.
int a[][]={{4,5,6},{1,2,3},{7,8,9}};
b)How are the following passed to a function?i)composite data type ii)primitive data type i)call by value ii)call by reference
c)a method definition is given below:-
int power(int x,int y)
{int f=1;for(int i=1;i<=y;i++)f*=x;return f;}
i)What is the term give to variable x ii)Name the type of method i)formal parameter ii)returnable/parameterised function
d)Write a java expression to create an object in which furniture is the class of the object chair.
furniture chair=new furniture();
e)char vowel[]={‘a’,’e’,’i’,’o’,’u’}; write a statement to replace u with v
vowel[4]=’v’;
f) Find and correct the errors:-
int n[]=(2,4,6,8,10);for(int i=0;i<=5;i++)System.out.println(“n[+”+”}=n[i]);
int n[]={2,4,6,8,10};for(int i=0;i<5;i++)System.out.println(“n[i]=”+n[i]);
g) class area
{int l,b;area{l=0;b=0;}area(int l,int b){this.l=l*l;this.b=b*b;}
void display(){System.out.println(“Area=”+(l*b));} public static void main(){area a1=new area();area a=new area(5,6); a.display();}} i)Name the
formal parameter(s) ii)use of this keyword iii)Value stored in l and b iv)What will be the output
i)l,b ii)name space collisions iii)l=25 b=36 iv)Area=900
h) String s[]={“mango”,strawberry”,”cherry”};
a)s.length b)s.length-2.length(); a)3 b)-3
i) int a[]={10,20,30,40,50}; a[0]=a[a.length-3];a[2]=[a.length-1];
for(int i=0;i<a.length;i++)System.out.print(a[i]+” “);
30 20 50 40 50
j) int check(int x,int y)
{int a,s,l;if(x<y){s=x;l=y;}
else {s=y;l=x;}
a=l;
while(a%s!=0)
{a+=1;}
return a;}
i)What value will be returned for function check(6,5) 10
ii)Write the main block to send the values and print the returned value.
public static void main()
{check1 ob=new check1();int s=ob.check(5,6);
MOCK SET-4 FUNCTIONS ,CONSTRUCTORS & ARRAYS FM:-40
Q.1.Choose the correct answer:-(1X20=20M)
a) The first line of a method which contains the access specifier, return type, method name and a list of parameters is termed
as_____________. ( method signature, method prototype or method header , formal parameter , actual parameter)
b) The parameters described in the method header which receive the values from its caller method are called _____________. ( formal
parameter , actual parameter , original parameter , none of the above)
c) Consider the following code and state which one is correct statement about the following code. abc p = new abc(5,7,9); ( method
calling with actual parameter , method declaration with formal parameter , declaration of parameterized constructor , declaration of
non-parameterized constructor )
d) The array int x[10] occupies: (10 bytes,40 bytes, 20 bytes, 8 bytes )
e) A method that does not return a value has a ____________ return type.( double,float,void,class)
f)A method can return ____(any number of values,only 1 value,2 values,3 values)
g)If a method returns a value,then it must be _____________(of the same data type as defined in its prototype,double type,void
type,Boolean type)
h)Parameters in the method definition are called__________(actual,formal,informal,void)
i)The parameters that are passed to the method when its invoked are called_____________(formal,actual,informal,void)
j)The scope of local variables is limited to_______(windows, class, method or block it is declared in, multiple programs)
k)The technique in which the change in the formal parameter gets reflected in the actual parameter is known as_____________.(call by
reference,call by value,call by argument,call by method)
l)A method with many definitions is called__________(many method,multiple method,void method,overloaded method)
m)A constructor is used when an object is _______________(created,destroyed,assigned a value,abstracted)
n)Pick the correct statement:-
(All constructors must have the same name as that of the class,all constructors must have the same number of arguments,all
constructors must have arguments of type String[] args,All constructors must have no arguments)
o)The number of bytes occupied by a character array of 10 elements.(20,60,40,120)
p)Java statement to access the 5th element of an array is-(x[4],x[5],x[3],x[0])
q)Constructor overloading follows which principle of OOP?(inheritance,polymorphism,abstraction,encapsulation)
r)boolean b[]={true,false};System.out.println(b[1]*2);What will be the output of the above code fragment?(2,0,true*false,syntax error)
s)A method prototype name check() which takes an integer argument and returns a char(char check(),void check(int x),check(int x),char
check(int x)
t) The error ArrayIndexOutOfBound is ______________ error[logical,syntax,semantical,runtime]
Q.2.Do as directed:-(2X10=20M)
a)Consider the following code and answer the following questions: [2]
public class circle { static double pi; double r,a,c; circle( ) { r=5.0; a=c=0.0; pi=Math.PI; } }
a) Name the instance variables. b) Name the class variable. a)r,a,c b)pi
b) Consider the following array and answer the following questions: [2]
int x[ ] ={23 , 45 , 67 , 12 , 45, 89 , 24 , 12 , 9 , 7}; n=10 y=90
a) int n=x.length; What will be the value of n? b) int y=x[1] + x[4]; What will be the value of y?
c) Consider the array int x[]={4,7,9,66,72,0,16}; i)What is the length of the array? Ii)what is the value in x[4] a)7 b)72
d) Write the dry-run and output:
void find(int a,int b){int p=1;for(int j=1;j<=b;j++)p=p*a;System.out.println(p);}If find(5,3) is invoked
125
e) Write the dry-run and output:
class Test { private static int x=0; public static void main() { System.out.println(fun()); } static int fun() { return ++x; }}
1
f) Write the correct form
void sum(int a, int b){return (a+b);}public static void main(){klass c1=new klass();Klass.sum(2.3,4.5);}}
int sum(int a, int b){return (a+b);}public static void main(){klass c1=new klass();int c=klass.sum(2.3,4.5);}}
g) Write the dry-run and output:
class animal{animal(){find();System.out.print(“Dog”);}void find(){display();System.out.print(“the animal”);}void display()
{System.out.print(“I love the”);}public void main(){animal ob=new animal();}}
I love thethe animalDog
h) Write the output:-
String
wd[]={“INDIA”,”USA”,”NEPAL”,”GERMANY”,”MEXICO”};System.out.println(wd[2].toLowerCase());System.out.println(wd[3].length());Sy
stem.out.println(wd[1]+wd[4]); i)Nepal ii)7 iii)USAMEXICO
i) Write the output:-int a[]={3,5,1,7,8};int x=++a[2];int y=a[3]++;System.out.println(x+” “+y); 2 7
j) Given the following declarations:
final int SIZE=20;char[] name=new char[SIZE]; i)name[0]=’D’; ii)System.out.println(name[9]);
iii)for(i=0;i<20;i++)
{name[i]=’ ‘;}
i)Write an assignment statement that stores ‘D’ into the first element of the array name.ii)Write an output statement that prints the
value of the tenth element of the array name.iii)Write a for statement that fills the array name with spaces

You might also like