Advertisement
udaykumar1997

Untitled

Sep 1st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.73 KB | None | 0 0
  1. --------------------------
  2. Post Title '.NET Assignment-1 Answers'
  3. Post by user 'Adusumilli-Uday-Kumar'
  4. Post encryped with secure 256-bit MD5 Encryption
  5. Post accesable only VIA link
  6. Post will Expire on '3-OCT-2017'
  7. --------------------------
  8.  
  9.  
  10.  
  11. ----
  12. 1. What is an Identifier? What's the syntax rule for Identifier?
  13. Ans. Identifiers are the names that you use to identify the elements in your programs, such as namespaces, classes, methods, and variables. In C#, you must adhere to the following syntax rules when choosing identifiers:
  14. - You can use only letters (uppercase and lowercase), digits, and underscore
  15. characters.
  16. - An identifier must start with a letter or an underscore.
  17. For example, result, _score, footballTeam, and plan9 are all valid identifiers, whereas result%, footballTeam$, and 9plan are not.
  18.  
  19. ----
  20. 2. What is a Variable? Write a note on variable naming Format. How to name a Variable?
  21. Ans. A variable is a storage location that holds a value. You use a variable’s name to refer to the value it holds.
  22.  
  23. Naming variables:
  24. You should adopt a naming convention for variables that helps you avoid confusion concerning the variables you have defined. The following list contains some general recommendations:
  25. - Don’t start an identifier with an underscore.
  26. - Don’t create identifiers that differ only by case.
  27. - Start the name with a lowercase letter.
  28. - In a multiword identifier, start the second and each subsequent word with an uppercase letter.
  29. - Don’t use Hungarian notation.
  30.  
  31. Declaring variables:
  32. Variables hold values. C# has many different types of values that it can store and process—integers, floating-point numbers, and strings of characters, to name three. When you declare a variable, you must specify the type of data it will hold. You declare the type and name of a variable in a declaration statement. For example, the statement that follows declares that the variable named age holds int (integer) values will look as follows :
  33. -
  34. int age;
  35. -
  36.  
  37. ----
  38. 3. What are the Primitive datatypes? Explain with Examples.
  39. Ans. C# has a number of built-in types called primitive data types. The following table lists the most commonly used primitive data types in C# and the range of values that you can store in each.
  40.  
  41. Table Link : http://bit.ly/2xCEHL2
  42.  
  43. ----
  44. 4. What are Airthematic Operators? Explain Operators and Types.
  45. Ans. C# supports regular arithmetic operations such as the plus sign (+) for addition, the minus sign (–) for subtraction. The symbols +, –, *, and / are called operators because they “operate” on values to create new values. In the following example, the variable 'mo' ends up holding the product of 750 and 20:
  46. -
  47. long mo;
  48. mo = 750 * 20;
  49. -
  50.  
  51. Operators and Types:
  52. Not all operators are applicable to all data types. The operators that you can use on a value depend on the value’s type. For example, you can use all the arithmetic operators on values of type char, int, long, float, double, or decimal. However, with the exception of the plus operator (+), you can’t use the arithmetic operators on values of type string, and you cannot use any of them with values of type bool.
  53.  
  54. ----
  55. 5. What is Controling Precedence? Explain Associativity to evaluate Exressions.
  56. Ans. Precedence governs the order in which an expression’s operators are evaluated. You can use parentheses to override precedence and force operands to bind to operators in a different way. For example, in the following expression, the parentheses force the 2 and the 3 to bind to the + operator (making 5), and the result of this addition forms the left operand of the * operator to produce the value 20:
  57. -
  58. (2 + 3) * 4
  59. -
  60.  
  61. Using associativity to evaluate expressions:
  62. Associativity is the direction (left or right) in which the operands of an operator are evaluated. Consider the following expression that uses the / and * operators:
  63. -
  64. 4 / 2 * 6
  65. -
  66. At first glance, this expression is potentially ambiguous. Do you perform the division first or the multiplication? The precedence of both operators is the same (they are both multiplicative), but the order in which the operators in the expression are applied is important because you can get two different results. In this case, the associativity of the operators determines how the expression is evaluated. The * and / operators are both left associative, which means that the operands are evaluated from left to right. In this case, 4/2 will be evaluated before multiplying by 6, giving the result 12.
  67.  
  68. ----
  69. 6. How to declare Local Variables Implicitly?
  70. Ans. You can declare and initialize a variable in the same statement, such as:
  71. -
  72. int myInt = 99;
  73. -
  74. Or, you can even do it like this, assuming that myOtherInt is an initialized
  75. -
  76. integer variable:
  77. int myInt = myOtherInt * 99;
  78. -
  79. Now, remember that the value you assign to a variable must be of the same type as the variable. You can also ask the C# compiler to infer the type of a variable from an expression and use this type when declaring the variable by using the var keyword in place of the type, as demonstrated here:
  80. -
  81. var myVariable = 99;
  82. var myOtherVariable = "Hello";
  83. -
  84. The variables myVariable and myOtherVariable are referred to as implicitly typed variables. The var keyword causes the compiler to deduce the type of the variables from the types of the expressions used to initialize them. In these examples, myVariable is an int, and myOtherVariable is a string. However, this is a convenience for declaring variables only, and that after a variable has been declared you can assign only values of the inferred type to it—you cannot assign float, double, or string values to myVariable at a later point in your program, for example. You can use the var keyword only when you supply an expression to initialize a variable.
  85.  
  86. ----
  87. 7. What is a Method? Write the syntax for declaring a Method.
  88. Ans.
  89. Creating methods:
  90. A method is a named sequence of statements. A method has a name and a body. The method name should be a meaningful identifier that indicates the overall purpose of the method. The method body contains the actual statements to be run when the method is called. Additionally, methods can be given some data for processing and can return information, which is usually the result of the processing. Methods are a fundamental and powerful mechanism.
  91.  
  92. The syntax for declaring a C# method is as follows:
  93. -
  94. returnType methodName ( parameterList )
  95. {
  96. // method body statements go here
  97. }
  98. -
  99.  
  100. The following is a description of the elements that make up a declaration:
  101. - The returnType, is the name of a type and specifies the kind of information the method returns as a result of its processing.
  102. - The methodName, is the name used to call the method. Method names follow the same identifier rules as variable names.
  103. - The parameterList, is optional and describes the types and names of the information that you can pass into the method for it to process.
  104. - The method body, statements are the lines of code that are run when the method is called. They are enclosed between opening and closing braces.
  105.  
  106. ----
  107. 8. Explain Expresion-Bodied Methods?
  108. Ans. Some methods can be very simple. C# supports a simplified form for methods that comprise a single expression. These methods can still take parameters and return values, and they operate in the same way as the methods that you have seen so far. The following code examples show simplified versions of the addValues and showResult methods written as expression-bodied methods:
  109. -
  110. int addValues(int leftHandSide, int rightHandSide) => leftHandSide + rightHandSide;
  111. void showResult(int answer) => Console.WriteLine($"The answer is {answer}");
  112. -
  113. The main differences are the use of the => operator to reference the expression that forms the body of the method and the absence of a return statement. The value of the expression is used as the return value; if the expression does not return a value, then the method is void. There is actually no difference in functionality between using an ordinary method and an expression-bodied method.
  114.  
  115. ----
  116. 9. Explain how a Method is Called? What's the syntax for calling a method?
  117. Ans. You call a method by name to ask it to perform its task. If the method requires information (as specified by its parameters), you must supply the information requested. If the method returns information (as specified by its return type), you should arrange to capture this information somehow.
  118.  
  119. The syntax of a C# method call is as follows:
  120. -
  121. result = methodName ( argumentList );
  122. -
  123.  
  124. The following is a description of the elements that make up a method call:
  125. - The methodName must exactly match the name of the method you’re calling.
  126. - The result = clause is optional. If specified, the variable identified by result contains the value returned by the method.
  127. - The argumentList supplies the information that the method accepts.
  128.  
  129. Examples:
  130. -
  131. int result = addValues(39, 3); // assignment
  132. showResult(addValues(39, 3)); // call
  133. -
  134.  
  135. ----
  136. 10. What is Applying Scope? Define Local Scope and Class Score.
  137. Ans. A variable can be used only after it has been created. When the method has finished, this variable disappears and cannot be used elsewhere. When a variable can be accessed at a particular location in a program, the variable is said to be in scope at that location.
  138. You can also define variables with different scope; for example, you can define a variable outside a method but within a class, and this variable can be accessed by any method within that class. Such a variable is said to have 'class scope'.
  139.  
  140. Defining local scope:
  141. The opening and closing braces that form the body of a method define the scope of the method. Any variables you declare inside the body of a method are scoped to that method; they disappear when the method ends and can be accessed only bycode running in that method. These variables are called local variables because they are local to the method in which they are declared; they are not in scope in any other method. The scope of local variables means that you cannot use them to share information between methods.
  142. Consider this example:
  143. -
  144. class Example
  145. {
  146. void firstMethod()
  147. {
  148. int myVar;
  149. ...
  150. }
  151. void anotherMethod()
  152. {
  153. myVar = 42; // error - variable not in scope
  154. ...
  155. }
  156. }
  157. -
  158.  
  159. This code fails to compile because anotherMethod is trying to use the variable myVar, which is not in scope. The variable myVar is available only to statements in firstMethod that occur after the line of code that declares myVar.
  160.  
  161. Defining class scope:
  162. The opening and closing braces that form the body of a class define the scope of that class. Any variables you declare within the body of a class (but not within a method) are scoped to that class. The proper C# term for a variable defined by a class is field. As mentioned earlier, in contrast with local variables, you can use fields to share information between methods.
  163. Here is an example:
  164. -
  165. class Example
  166. {
  167. void firstMethod()
  168. {
  169. myField = 42; // ok
  170. ...
  171. }
  172. void anotherMethod()
  173. {
  174. myField++; // ok
  175. ...
  176. }
  177. int myField = 0;
  178. }
  179. -
  180.  
  181. The variable myField is defined in the class but outside the methods firstMethod and anotherMethod. Therefore, myField has class scope and isavailable for use by all methods in that class. There is one other point to notice about this example. In a method, you must declare a variable before you can use it. Fields are a little different. A method can use a field before the statement that defines the field—the compiler sorts out the details for you.
  182.  
  183. ----
  184. 11. What is Method Overloading?
  185. Ans. If two identifiers have the same name and are declared in the same scope, they are
  186. said to be overloaded. However, there is a certain way that you can overload an identifier for a method, without causing compile-time errors that is both useful and important.
  187. At compile time, the compiler looks at the types of the arguments you are passing in and then arranges for your application to call the version of the method that has a matching set of parameters.
  188. Here is an example:
  189. -
  190. static void Main()
  191. {
  192. Console.WriteLine("The answer is ");
  193. Console.WriteLine(42);
  194. }
  195. -
  196. Overloading is primarily useful when you need to perform the same operation on different data types or varying groups of information.
  197.  
  198. ----
  199. 12. Explain Optional Parameters and Named Arguments.
  200. Ans. One of the principal technologies that underpins many Windows applications and
  201. services running outside the .NET Framework is the Component Object Model
  202. (COM). In fact, the common language runtime (CLR) used by the .NET
  203. Framework is also heavily dependent on COM, as is the Windows Runtime of
  204. Windows 10. COM does not support overloaded methods; instead, it uses
  205. methods that can take optional parameters. To make it easier to incorporate COM
  206. libraries and components into a C# solution, C# also supports optional
  207. parameters.
  208. Optional parameters are also useful in other situations. They provide a compact
  209. and simple solution when it is not possible to use overloading because the types
  210. of the parameters do not vary sufficiently to enable the compiler to distinguish
  211. between implementations.
  212.  
  213. ----
  214. 13. Define Boolean Operators and Conditional Logical Operators.
  215. Ans. A Boolean operator is an operator that performs a calculation whose result is either true or false. C# has several very useful Boolean operators, the simplest of which is the NOT operator, represented by the exclamation point (!). The ! operator negates a Boolean value, yielding the opposite of that value. In the preceding example, if the value of the variable areYouReady is true, the value of the expression !areYouReady is false.
  216.  
  217. Understanding conditional logical operators:
  218. C# also provides two other binary Boolean operators: the logical AND operator, which is represented by the && symbol, and the logical OR operator, which is represented by the || symbol. Collectively, these are known as the conditional logical operators. Their purpose is to combine two Boolean expressions or values into a single Boolean result. These operators are similar to the equality and relational operators in that the value of the expressions in which they appear is either true or false, but they differ in that the values on which they operate must also be either true or false. The outcome of the && operator is true if and only if both of the Boolean expressions it’s evaluating are true.
  219. For example, the following statement assigns the value true to validPercentage if and only if the value of percent is greater than or equal to 0 and the value of percent is less than or equal to 100:
  220. -
  221. bool validPercentage;
  222. validPercentage = (percent >= 0) && (percent <= 100);
  223. -
  224.  
  225. ----
  226. 14. Explain Short Circuiting in Conditional Logical Operator.
  227. Ans. The && and || operators both exhibit a feature called short circuiting. Sometimes, it is not necessary to evaluate both operands when ascertaining the result of a conditional logical expression. For example, if the left operand of the && operator evaluates to false, the result of the entire expression must be false, regardless of the value of the right operand. Similarly, if the value of the left operand of the || operator evaluates to true, the result of the entire expression must be true, irrespective of the value of the right operand. In these cases, the && and || operators bypass the evaluation of the right operand.
  228.  
  229. ----
  230. 15. Expalin If Statement and Cascading If Statement.
  231. Ans.
  232. if statement syntax:
  233.  
  234. The syntax of an if statement is as follows:
  235. -
  236. if ( booleanExpression )
  237. statement-1;
  238. else
  239. statement-2;
  240. -
  241.  
  242. If booleanExpression evaluates to true, statement-1 runs; otherwise, statement-2 runs. The else keyword and the subsequent statement-2 are optional. If there is no else clause and the booleanExpression is false, execution continues with whatever code follows the if statement. Also, notice that the Boolean expression must be enclosed in parentheses; otherwise, the code will not compile.
  243. Example:
  244. -
  245. if ( booleanExpression )
  246. statement-1;
  247. else
  248. statement-2;
  249. -
  250.  
  251. Cascading if statements:
  252. You can nest if statements inside other if statements. In this way, you can chain together a sequence of Boolean expressions, which are tested one after the other until one of them evaluates to true.
  253. Example:
  254. -
  255. if (day == 0)
  256. {
  257. dayName = "Sunday";
  258. }
  259. else if (day == 1)
  260. {
  261. dayName = "Monday";
  262. }
  263. else if (day == 6)
  264. {
  265. dayName = "Saturday";
  266. }
  267. else
  268. {
  269. dayName = "unknown";
  270. }
  271. -
  272.  
  273. ----
  274. 16. Explain Switch Statement, it's Syntax and it's rules.
  275. Ans.
  276. switch statement syntax:
  277. The syntax of a switch statement is as follows (switch, case, and default are keywords):
  278. -
  279. switch ( controllingExpression )
  280. {
  281. case constantExpression :
  282. statements
  283. break;
  284. case constantExpression :
  285. statements
  286. break;
  287. ...
  288. default :
  289. statements
  290. break;
  291. }
  292. -
  293.  
  294. The controllingExpression, which must be enclosed in parentheses, is evaluated once. Control then jumps to the block of code identified by the constantExpression whose value is equal to the result of the controllingExpression. (The constantExpression identifier is also called a case label.) Execution runs as far as the break statement, at which point the switch statement finishes and the program continues at the first statement that follows the closing brace of the switch statement. If none of the constantExpression values is equal to the value of the controllingExpression, the statements below the optional default label run.
  295.  
  296. the switch statement rules:
  297. The switch statement is very useful, but unfortunately, you can’t always use it when you might like to. Any switch statement you write must adhere to the following rules:
  298. - You can use switch only on certain data types, such as int, char, or string. With any other types (including float and double), you must use an if statement.
  299. - The case labels must be constant expressions, such as 42 if the switch data type is an int, ‘4’ if the switch data type is a char, or “42” if the switch data type is a string. If you need to calculate your case label values at run time, you must use an if statement.
  300. - The case labels must be unique expressions. In other words, two case labels cannot have the same value.
  301. - You can specify that you want to run the same statements for more than one value by providing a list of case labels and no intervening statements, in which case the code for the final label in the list is executed for all cases inthat list.
  302.  
  303. ----
  304. 17. Explain While Statement and For Statement.
  305. Ans.
  306. while statements:
  307. You use a while statement to run a statement repeatedly for as long as some condition is true. The syntax of a while statement is as follows:
  308. -
  309. while ( booleanExpression )
  310. statement
  311. -
  312.  
  313. The Boolean expression (which must be enclosed in parentheses) is evaluated, and if it is true, the statement runs and then the Boolean expression is evaluated again. If the expression is still true, the statement is repeated, and then the Boolean expression is evaluated yet again. This process continues until the Boolean expression evaluates to false, at which point the while statement exits. Execution then continues with the first statement that follows the while statement.
  314.  
  315. In C#, most while statements have the following general structure:
  316. -
  317. initialization
  318. while (Boolean expression)
  319. {
  320. statement
  321. update control variable
  322. }
  323. -
  324.  
  325. for statements:
  326. The for statement in C# provides a more formal version of this kind of construct by combining the initialization, Boolean expression, and code that updates the control variable. You’ll find the for statement useful because in a for statement, it is much harder to accidentally leave out the code that initializes or updates the control variable, so you are less likely to write code that loops forever.
  327. Here is the syntax of a for statement:
  328. -
  329. for (initialization; Boolean expression; update control variable)
  330. statement
  331. -
  332. The statement that forms the body of the for construct can be a single line of code or a code block enclosed in braces.
  333.  
  334. ----
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement