Help With C++ Multiple choice Qs 1-6

Can you please verify my answers.

1. Define
char str[]="Hello world"; cout << &str[6] << endl;

What will be the output of above code?

A. Hello world
B. Hello
C. world <--
D. w


2. Define
string str = "Hello " + "world"; cout << str << endl;

What will be the output of above code?

A. Hello world
B. Hello
C. world
D. Compilation error <--


3. Which statement is not correct?

A. Object similarity means those objects have same type of data.
B. A class can be considered as a model.
C. An object is an instance of a class or a structure.
D. Two instances of a class have different identities. <--




4. Define a class class Circle {

int x; int y; int r;

static double pi=3.14; public:
Circle(int=0, int=0, int=0); double area();
};

Which statement is correct?

A. Cannot assign value to pi in the class. <--
B. Cannot assign zeros to the parameters in the constructor.
C. Should define a default constructor.
D. Return type of area() should be int.


5. Which statement is correct?

A. Constructors must be public.
B. Constructors can be private. <--
C. Default copy constructor can do deep copy.
D. Default constructor initializes data members to zeros.

6. Which statement is not correct?

A. In a class, a static function member cannot use a non-static data member.
B. In a class, a static function member can use a static data member.
C. In a class, a non-static function cannot use a static data member. <--
D. In a class, a non-static function can use a static data member.
1. Define
char str[]="Hello world"; cout << &str[6] << endl;


that is illegal, IMO it should be :
char str[]="Hello world"; cout << str[6] << endl;

and the output of it is W

str[6] refers to the 7th letter in the string || ( 6th element in the array ) ( reading starts at 0 )

~~~~~~

2. Define
string str = "Hello " + "world"; cout << str << endl;

What will be the output of above code?


the output is Hello World

the string class has an overloaded operator + that concatenate and assigns the string literals "Hello " and "World" to str

~~~~~~
6. Which statement is not correct?


the options are quite confusing, a wild guess, A ?
Last edited on
@shadow fiend: rethink all three. It is legal to take an address of a memory location. While std::string has operators, there is no std::string in the crucial part of the question. 6A requires something, but is not always impossible.

@solaclips: Number 3 gives an eerie feeling.
yes i am wrong about question 1, haven't been to raw pointers for a while XD
Topic archived. No new replies allowed.