473,395 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

value of z in structured objects in ABC

10
Last time interview, I got this questions for test.
I would like to know which answer is correct.

Q: Assume you have a list "ABC" which contains structures of "XYZ". "ABC" has 10 "XYZ" objects in.
Each structure has only three integers (x,y and z). all x and y have been assigned values and z is zero.
there is a code snippet as shown below.
foreach(XYZ obj in ABC)
{
obj.z = obj.x;
obj.z = obj.z * obj.y;
}

what value you would expect to see in the structure objects in "ABC" after this ?
1) All z values in the structure Z will be zero
2) All z values in the structure Z will be equal to value x
3) Only the first structure in the list "ABC" will have z values equal to x*y
4) All z values in the structure Z will be equal to value x * y
Sep 30 '10 #1
2 1287
I think it should be option 1?
Sep 30 '10 #2
Joseph Martell
198 Expert 128KB
Since this is .Net, I believe that the answer is 4. The thought is as follows:

You said that ABC is a list of structures of XYZ. This is important because it implies that
  1. ABC is a generic collection of XYZ structures. Therefore the foreach statement is valid and no type conversion or boxing/unboxing is necessary.
  2. XYZ is a structure so it is a value type. This means that it cannot be NULL. Therefore it is impossible to have null reference exceptions in this code.

You also stated that the XYZ structure is composed of only three elements: x, y, and z. All three of these are integers. This also means that null reference exceptions are impossible inside the foreach loop.

The first assignment statement in the loop assigns z the value of x. The second statement resolves the right-hand side first (z * y) and assigns it to z. Since z has already been assigned the value of x, we can make the logical leap:

z = x
z = z * y
(plus a little algebra magic)
z = x * y

So I would choose answer 4, though if I were answering the question in person the fact that the answer says
All z values in the structure Z will be equal to value x * y
would make me nervous since the structures are of type XYZ, not Z. That is probably me just over thinking things.
Oct 1 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jon Franz | last post by:
----- Original Message ----- From: "Jon Franz" <jfranz@neurokode.com> To: "Serge Orlov" <sombDELETE@pobox.ru> Sent: Wednesday, November 19, 2003 2:39 PM Subject: Re: Python Database Objects (PDO)...
11
by: thechaosengine | last post by:
Hi all, I have a very general but quite significant question about objects. My question is, when should I create them? I know thats a crap question so let me explain a bit further. Lets...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
1
by: Shail | last post by:
I want to store the value of objects in View Statr but it gives exception that object is not Serializable, I added serializable attribute before the class it didn't work. What must be done?...
9
by: Peter Oliphant | last post by:
I've been told that value structs can have default constructors (I'm using VS C++.NET 2005 PRO using clr:/pure syntax). But the following code generates the following error: value struct...
12
by: Edward Diener | last post by:
Given value class X { public: // Not allowed: X():i(100000),s(10000) { } // Allowed void InitializeDefaults() { i = 100000; s = 10000; } private: int i;
0
by: John | last post by:
Hi, I have set a DataGridViewComboBoxColumn for a complex object type, and set the DisplayMember and ValueMember to members of the object type. My problem is how to programmatically set the cell...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
5
by: =?Utf-8?B?RGF2aWQgTGV2aW5l?= | last post by:
This is really more of a "why was C# designed this way?" type of question. Hope this is the write forum for this question. Given the following : class Base1 { public int i; } struct Base2...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.