Wipro Technologies Interview Question for Software Analysts

Country: India
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
of 1 vote

This is a compiler specific problem, in most compilers const_casting to non const variable and then changing the value is 'undefined', so this is a terrible question to ask in an interview, however in most compilers what happens is as follows:

A) The public scope variable which has a explicit value assigned to it can be determined by compiler at compile time and is replaced at "compile time" by literals, i.e. the
"100", or more specifically, depending on compiler, to an address in code segment instead of data segment, so there is no actual memory allocated for temp. Changing the value of temp to 2000 in "fun" is an attempt to change an undefined memory or a memory in code segment which causes the crash. This is not for all cases, if we change the code to something like this:

// class A{ ...

int another_var = 100;
const int temp= another_var;

int main()
// { ...

then there will be actual memory allocated to temp and the program does not crash, even though the const variable is still in global scope. but it all depends on the cleverness of the compiler.

B) In the other hand, local consts inside functions are always allocated actual memory in most compilers, this is probably because of efficiency in accessing local variable in function's memory in stack in comparison to a value in code segment. So again, same happens as example above and we won't have any crash.

- Sina January 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

Global constant created on read only data segment of memory so you can not modify its value ... variable inside main created on stack to it can be modified ...

- MI December 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 votes

A.) The scope of 'temp' is global and is of type constant so we cannot change the value of a function inside the function fun()
B.) The way the temp argument passed to fun is by using "const_cast" which removes constness of the variable 'temp'.

- Durga December 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
of 0 votes

@Durga, In the following program, why the assignment in fun() "temp=2000" only casues crash and not the assignment in main "b = 30"?

#include <iostream>
#include <stdio.h>
using namespace std;
class A{
		void fun(int& temp){
			cout<<"1am inside fun()"<<endl;
			temp = 2000;
			cout<<"2am inside fun()"<<endl;
			printf("in fun() temp[%p] temp val[%d]\n",&temp, temp);

const int temp=100;
int main()
	A a;
	printf("in main() temp[%p] temp val[%d]\n",&temp, temp);
	int b = const_cast<int&>(temp);
	b = 30;
	cout << "b:" << b << endl;

- SantiagoYemmiganur January 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
of 0 vote

Agreed with Sina regarding the reason for the crash & why moving temp decl inside main 'fixes' it. But no one's answered the other part of the question -- once change is made, why does observed value change for fun() but not for main() ?

That's because optimizing compilers will typically cache data in processor registers when they have no reason to think the data will change (if a register is available). Declaring temp 'const' explicitly tells the compiler it's OK to do that. So main copies the initial value of temp to a register, and never re-loads it from memory; fun() changes the value in memory, but that new value is never read by main().

- Karl.Zimmerman@utas.utc.com September 08, 2014 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More