2/19/2011

DCT1063 P2 PRE-TEST

A

The &
operator will returns the memory
address of its operand.

T/F

B 

The * operator
will returns the value of the variable located at the address specified by its operand.

T/F

C 

In C++, there is a close relationship between pointers and arrays.

T/F

D 

Consider the following fragment:

int no[3]={0,1,2};

int *p1;

p1 = no;

the statement no[1] or *(p1+1) will output the different result.

T/F

E 

Both statements (in D) will return the 1st element of array no .

T/F

F 

In order to perform file I/O, you must include the header <string.h>
in your program.

T/F

G 

To open an output stream, you must declare the stream to be of class ofstream.

T/F

H 

A stream that will be performing both input and output operations must be declared as class ifstream.

T/F

I 

The following fragment opens a file called out;

ofstream out;

out.open("test.txt");

T/F

J 

To close a file, use the member function close( ).

T/F


 

SECTION B –Show Output

1. What is output by the following program segment?


 

1 int number = 9;

2 int *p = &number; // address of number is 0010FF8C

3

4cout << number << " " << *p << " " << p;

(4 Marks)

answer


 


 


 


 


 

2. What is output by the following program segment?


 

1 int a[] = { 1, 3, 5, 7, 9 };

2 int *p = a;

3

4 cout << a[ 0 ] << " " << *( p + 1 ) << " " << p[ 2 ];

(4 Marks)


 

answer:


 


 


 


 

3. What is output by the following program segment?


 

1 int a[] = { 1, 2, 3, 4, 5 };

2 int *p = a;

3

4 cout << a[ 0 ];

5 cout << *(a+1);

6 cout << *(p + 2);

7 cout << p[ 3 ];

(5 Marks)


 

answer:


 


 


 


 


 


 


 


 

4. What is output by the following program segment?


 

1 int i,a[] = { 1, 2};// address of a[0] is 5000

2 int *p = a;

3

4 for(i=0;i<=1;i++)

5

6 cout << (p+i)<<" "<<*(p+i)<<endl;

(3 Marks)


 

answer


 


 


 

SECTION C –Programming



 

  1. Consider the following C++ program. Show the content of test.txt


    #include <iostream>

    #include <fstream>

    using namespace std;

    int main()

    {

    ofstream out("test.txt");

    out << 10 << " " << 123.23 << "\n";

    out << "This is a short text file.";

    out.close();

    return 0;

    }


 


 


 

2. Write a program that prompts user to enter:-


 

  1. Name
  2. Age
  3. State
  4. Country


 

and write into a file named biodata.txt as follows.


 

Name     : Ahmad Ashraaf

Age    : 19

State: terengganu

Country: Malaysia


 


 


 

(10 Marks)

No comments:

Cara download Installer windows 10 dalam format ISO

1. Jika anda bercadang untuk download windows 10 melalui website rasmi windows - pilihan untuk download dalam format ISO tidak di berikan.  ...