Прашања/квиз - функции


Од учениците III4/5


Oд учениците III 7 


Quiz: Functions  

1. Which is not a proper prototype?

A. int funct(char x, char y);
B. double funct(char x)
C. void funct();
D. char x();

2. What is the return type of the function with prototype: "int func(char x, float v, double t);"

A. char
B. int
C. float
D. double

3. Which of the following is a valid function call (assuming the function exists)?

A. funct;
B. funct x, y;
C. funct();
D. int funct();

4. Which of the following is a complete function?
A. int funct();
B. int funct(int x) {return x=x+1;}
C. void funct(int) {cout&tl;<"Hello"}
D. void funct(x) {cout<<"Hello"}

1. A function can return more than one value.
a.True b. False

2.A function needs function definition to perform a specific task.
a.True b. False

3. If a function returns no value, the return type must be declared as void.
a.True b. False

4. A local variable declared in a function is not usable out side that function.
a.True b. False

5. Find any errors in the following function definition:
void myfunction(int x,int y)
{
cout<<x*y;
return(x*y);
}

6. Find any errors in the following function definition:
int myfunction(int x,y){
return(x*y);
}

7. Find any errors in the following function prototypes:
a.int sum(int x,y);
b.int sum(int x,int y)
c.int sum(int x,void y);

8. What would be the output from the following C++ program when run using 3 4 as input data?
#include<iostream>
#include<conio.h>  
//It is a header file used in c and cpp and it includes inbuilt functions like getch() and clrscr()
using namespace std;
int subtr(int x,int y);
int main(){
int x,y,res;
cout<<"Enter x and y separated by space:";
cin>>x>>y;
res=subtr(x,y);
cout<<"Result="<<res;
getch();
return 0;
}

int subtr(int x,int y){
return(x-y);
}

9.What would be the output from the following C++ program when run using 2 4 1 as input data?
#include<iostream>
#include<conio.h>
using namespace std;
int max3(int x,int y,int z);
int main(){
int x,y,z,max;
cout<<"Enter x, y and z separated by space:";
cin>>x>>y>>z;
max=max3(x,y,z);
cout<<"The max value is "<<max;
getch();
return 0;
}

int max3(int x,int y,int z){
if(x>y)
if(x>z) return x;
else return z;
else if(y>z) return y;
else return z;
}

10.What would be the output from the following C++ program when run a.using 3 2 as input data? b. using 3 -2 as input data?  // рекурзија
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std ;
double mypower(int base,int pow);
int main(){
int b,p;
double res;
cout<<"Enter base value and its power separated by space:";
cin>>b>>p;
res=mypower(b,p);
cout<<"Result="<<res;
getch();
return 0;
}

double mypower(int base,int pow){
if(pow>=0){ //positive power
if(pow==0) return 1;
else if(pow==1) return base;
else return (base*mypower(base,pow-1));
}
else{ //negative power
if(pow==-1) return(1/(double)base);
else return((1/(double)base)*mypower(base,pow+1));
}
}


Прашања со одговори 
1. Where does the execution of the program starts?
a) user-defined function
b) main function
c) void function
d) else function 
Аnswer: B
Explanation: Normally the execution of the program in c++ starts from main only.
2. What are mandatory parts in the function declaration?
a) return type, function name
b) return type, function name, parameters
c) parameters, function name
d) parameters, variables
Answer: A
Explanation: In a function, return type and function name are mandatory all else are just used as a choice.
3. which of the following is used to terminate the function declaration?
a) :
b) )
c) ;
d) ]
Answer: C
Explanation: ; semicolon is used to terminate a function declaration statement in C++.
4. How many can max number of arguments present in function in the c99 compiler?
a) 99
b) 90
c) 102
d) 127
Answer: D
Explanation: C99 allows to pass a maximum of 127 arguments in a function.
5. Which is more effective while calling the functions?
a) call by value
b) call by reference
c) call by pointer
d) call by object
Answer: B
Explanation: In the call by reference, it will just passes the reference of the memory addresses of passed values rather than copying the value to new memories which reduces the overall time and memory use.
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void car{
    cout << "Audi R8";
}
int main() {
    car();

    return 0;
}
a) Audi R8
b) Audi R8Audi R8
c) compile time error
d) runtime error
Answer: C
Explanation: We have to use the semicolon to declare the function in line 3. This is called a function declaration and a function declaration ends with a semicolon.
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void fun(int x, int y){
    x = 20;
    y = 10;
}
int main() {

    int x = 10;
    fun (x, x);
    cout << x;
    
    return 0;
}
a) 10
b) 20
c) compile time error
d) 30
Answer: A
Explanation: In this program, we called by value so the value will not be changed, So the output is 10
8. What is the scope of the variable declared in the user defined function?
a) whole program
b) only inside the {} block
c) the main function
d) header section
Answer: B
Explanation: The variable is valid only in the function block as in other.
9. How many minimum number of functions should be present in a C++ program for its execution?
a) 0
b) 1
c) 2
d) 3
Answer: B
Explanation: The execution of a C++ program starts from main function hence we require atleast 1 function to be present in a C++ program to execute and i.e. the main function.
10. Which of the following is the default return value of functions in C++?
a) int
b) char
c) float
d) void
Answer: A
Explanation: C++ uses int as the default return values for functions. It also restricts that the return type of the main function must be int.
Што ќе се прикаже по извршување на следната програма  



















Објасни !



 Која вредност ќе се прикаже со следниот код ако има вредност 25?

if (round(sqrt(n)) == sqrt (n))

cout<<sqrt(n);

else

cout<<n;


  Што ќе се отпечати со следниот програмски код во програмскиот јазик С++?

#include <iostream>

#include <cmath>

using namespace std;

int main()

 {

double a,b;

a= -3.7;

b=2.8;

cout<<floor(abs(a)+b);

return 0;   }

 Од кој тип е повратната вредност на функцијата pv во следниот програмски код?

int pv(double a)

{  float b;

b=2*a;

return b;  }

   Со кој број е означена наредбата за дефинирање прототип на функција во следниот програмски код во С++?

1.       #include <iostream>

2.       using namespace std;

3.       void mojaFunkcija();

4.       int main()

5.       {

6.       cout<<”Vo funkcijata main”<< endl;

7.       mojaFunkcija();

8.       cout<< “povtorno vo main”<<endl;

9.       return 0;

10.   }

11.   void mojaFunkcija()

12.   {

13.   cout<<”Vo funcijata mojaFunkcija”<<endl; }


 Што ќе се прикаже на екранот по извршување на следниот програмски код?

 #include <iostream>

using namespace std;

void mojaFunkcija();

int main()

{

cout<<”Vo funkcijata main”<< endl;

mojafunkcija();

cout<< “povtorno vo main”<<endl;

return 0;

}

void mojaFunkcija()

{

cout<<”Vo funcijata mojaFunkcija”<<endl;

Определи кој е резултатот од извршување на следниот програмски код.

  #include <iostream>

  using namespace std;

   void pecati(int i)

   {         cout << i;  }

    void pecati(double  f)

    {         cout << f;  }

    int main(void)

    {

        pecati(5);

        pecati(500.263);

        return 0;   }

Определи кој е резултатот од извршувањето на следниот програмски сегментво програмскиот јазик С++.

float a, b, c;

b = 3.456;

a = floor(b);

c = ceil(b);

cout << “a=”<<a<<”c=”<<c<<endl;

  Што ќе се прикаже по извршување на следниот програмски сегмент?

float a, b, c, d, e;

a= 9; b = 3;

d = sqrt (b*3);

e =pow(a,2);

c = d+e;

cout<<”d=”<<d<<endl;

cout<<”e=”<<e<<endl;

cout<<”c=”<<c;

Која вредност ќе се отпечати по извршување на следните наредби ако целобројната променлива има вредност 64?

if (round(sqrt(n)) == sqrt(n))

        cout<<sqrt(n);

else  cout<<n;

Дефинирана е целобројна функција g со два целобројни аргументи x и y.

int g (int x, int y)

{

int s;

y = y % 5;

s = 2 * x - y;

return (s);

}

Колкава ќе биде вредноста на целобројната променлива по извршување на следната наредба?

t = g (4, g (15, 38));

Каков вид параметри се а и во слeдниот програмски сегмент во програмскиот јазик С++?

 #include <iostream>

 using namespace std;

 int max(int a,int b)

 {

int m;

 if (a>b)

    m=a;

 else

         m=b;

   return m;

   }

   int main ()

   {

    int x,y;

   cin>>x>>y;

   cout<<max(x,y);

}

Кој е минималниот број на функции кои треба да ги има една програма во програмскиот јазик С++?

Што ќе се прикаже по извршување на следниот програмски код во програмскиот јазик С++?

#include <iostream>

using namespace std; 

void f(int a)

 {

a++;

cout<<a<<”,”;

}

int main()

{

int a=4;

f(a);

cout<<a*2;

return 0;

}

 Ана дефинирала функција на следниот начин.

int f (int x)

{

     if (x%2)

        return x+1;

      else

        return x-1;

}

Што ќе се прикаже на екранот кога Ана ќе ја повика функцијата со наредбата f(5)?

Што ќе се отпечати со следниот програмски код во програмскиот јазик С++?

int funkcija (int x, int y)

{

x = x + y;

return x - y;

}

int main ()

{

int x = 2, y = 3;

cout<<x-funkcija (x, y);

return 0;

}

Што се декларира со следниот изказ во програмскиот јазик С++?

double Cena( int x );

Што ќе се отпечати со следниот програмски сегмент ако x и y се реални променливи во програмскиот јазик С++? 

x = 2.7; y = 2.3;

x = 2 * y;

if (floor(x) < round(y))

cout<< x;

else

     if (round(x) > 3 * floor(y))

cout<< y;

else

cout<< floor(y)+round(x);           

 Што ќе се прикаже на екранот како резултат од извршувањето на следниот програмски сегмент во програмскиот јазик С++?

   #include <iostream>

    using namespace std;

    void pari()

    {

        cout<<"valuta";

    }

    int main()

    {

        pari ();

        return 0;

    }

Која вредност ќе ја добие променливата x по извршување на следниот програмски сегмент?

    #include <iostream>

    using namespace std;

    void fun(int x, int y)

    {

        x = 20;

        y = 10;

    }

    int main()

    {

        int x = 10;

       fun(x, x);

        cout << x;

        return 0;

    }

Кој е резултатот што се добива со извршување на следната функција дефинирана во програмскиот јазик С++?

#include <iostream>

using namespace std;

 int addition (int a, int b)

{

  int r;

  r=a+b;

  return r;

}

int main ()

{  

int z;

  z = addition (5,3);

  cout<<z;

}

  Што ќе се прикаже на екранот како резултат од извршувањето на следниот програмски сегмент, за n=5?

#include<iostream>

using namespace std;

int suma(int n)

{

     int s;

     if(n==0)

        s=0;

     else    

    s=n+suma(n-1);

     return s;

}

main()

{

      int n;

      cin>>n;

      cout<< suma(n)<<endl;

return 0; }

Што ќе се прикаже на екранот како резултат од извршувањето на следниот програмски сегмент?

#include<iostream>

#include<cmath>

using namespace std;

 int main()

{

float a,b;

a= 5.8;

b= 5.3;

if (floor (a) == round (b))

cout <<"a="<<ceil(++a);

else  cout <<"b="<< floor (--b);

return 0;

}

 Што ќе се прикаже на екранот како резултат од извршувањето на следниот програмски сегмент?

#include <iostream>

using namespace std;

int f1(int t, int &u, int &v)

{

t = t+3;

u = u+1;

v =v - t + 2*u;

return v;

}

int main()

{

int a=1, b=2, c=3;

cout <<" Vrednosta na funkcijata iznesuva "<< f1(a,b,c) << endl;

cout <<" a vrednosta na  a="<< a <<"   b="<< b <<"   c="<< c << endl;

return 0;

}

 Што ќе се прикаже на екранот како резултат од извршувањето на следниот програмски сегмент?

#include <iostream>

using namespace std;

int f1(int &t, int u, int &v)

{

t = 2*t+1;

u = u+1;

v = v + t + 2*u;

return v;

}

int main()

{

int a=1, b=2, c=3;

cout << "Funkcijata ima vrednost "<<f1(a, b, c)<< endl;

cout <<" Vrednostite za a, b  i  c se "<< a << " " << b << " " << c << endl;

return 0;

}

 Што ќе се прикаже на екранот како резултат од извршувањето на следниот програмски сегмент, ако n=11, m=7

#include <iostream>

using namespace std;

void   y2 (int n, int m)

{

int c;

if ( n >= m)

c = n%2;

else c =m*2;

cout <<"c="<< c <<endl;

}

int main()

{

int n,m;

cin>>n>>m;

y2(n,m);

return 0;

}

Одговори кој е резултатот од извршување на следниот програмски сегмент.

void duplicate (int& a, int& b, int& c)

{

  a*=2;              

  b*=2;             

  c*=2;             

}

int main ()

{

  int x=1, y=3, z=7;

duplicate (x, y, z);

  cout << "x=" << x << ", y=" << y << ", z=" << z;

  return 0;

}

Како треба да се дефинираат рефенците во програмскиот код прикажан на сликата за да се добие следниот резултат x=1, y=6,z=14?

void duplicate (int a, int b, int c)

{

  a*=2;             

  b*=2;             

  c*=2;            

}

int main ()

{

  int x=1, y=3, z=7;

duplicate (x, y, z);

  cout << "x=" << x << ", y=" << y << ", z=" << z;

  return 0; }


No comments: