.Net 1 Lab

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Which of the following statements are true about partial methods? Each correct answer represents a complete solution. Choose all that apply.

-They are always private and can't have a return value. -They are defined in one partial class definition without a method body.

Which of the following is not true about exception handling in C#?

A program can contain multiple finally clauses.

Which of the following are instructions that can interrupt application execution with a user‐defined message?

Assertions

What are the features of dependency properties?Each correct answer represents a complete solution. Choose all that apply. This task contains the radio buttons and checkboxes for options. The shortcut keys to perform this task are A to H and alt+1 to alt+9. A -Styles cannot be used to change the value. B -Its values can be changed in an animation. C -Its values can be set using either resources or by data binding. D -Metadata cannot be applied to a dependency property.

B -Its values can be changed in an animation. C -Its values can be set using either resources or by data binding

Which of the following is the correct statement about an exception? A Occurs during Just-In-Time compilation B Occurs at run time C All of these D Occurs during loading of program

B Occurs at run time

Which of the following binding components specifies the object on which binding is used?

Binding target

Which layout control holds a single child that will be sized to completely fill this control?

Border

How a developer is able to display a message on the output window without a temporary pause at the point of debugging?

By adding a tracepoint

Why is PCL taken into consideration?Each correct answer represents a complete solution. Choose all that apply. This task contains the radio buttons and checkboxes for options. The shortcut keys to perform this task are A to H and alt+1 to alt+9. A -For porting code from .Net Framework to .NET Core B -For managing memory allocation, compilation, and execution C -For targeting multiple verticals and platforms D -For resolving the scaling, maintaining, testing, and supportability issues

C -For targeting multiple verticals and platforms D -For resolving the scaling, maintaining, testing, and supportability issues Using the directives #if, #else, and #endif to provide cross platform support was never a solid, scalable, maintainable, or easily supportable approach. The program ended up with many code paths, required much updating and complicated testing procedures when the components of the software or hardware were changed. As the need for targeting multiple verticals and platforms increased, a new solution for cross platform support was in high demand. From that demand, the concept of Portable Class Libraries (PCL) was created. PCL helped resolve the scaling, maintaining, testing, and supportability issues to a considerable extent.

What kind of information is transferred to the WPF when a class implements the INotifyPropertyChanged interface?

Changes were made in the property of the class

Question 7 :Consider the following code: public partial class MyClass { partial void DoSomethingElse(); public void DoSomething() { WriteLine("DoSomething() execution started."); DoSomethingElse(); WriteLine("DoSomething() execution finished."); } } public partial class MyClass { partial void DoSomethingElse() => WriteLine("DoSomethingElse() called."); What will be the output if we remove the second partial class definition from the given code?

DoSomething() execution started.DoSomething() execution finished.

Which of the following statements is true about a ComboBox?

Enables a user to select items from the list or enter a new value

Which of the following statements is correct regarding the given C# program? using System; struct StructEx { int x = 10; //Line 1 int y = 20; //Line 2 } class MyClient { public static void Main() { StructEx s1 = new StructEx(); //Line 3 int add = s1.x + s1.y; Console.WriteLine(add); } }

Error will occur at lines 1 and 2.

What will the output of the code be? using System; class MyProgram { static void Main(string[] args) { try { int a, b; b = 0; a = 5 / b; Console.WriteLine("No exception will occur."); } catch (ArithmeticException e) { Console.WriteLine("Exception occurs."); } finally { Console.WriteLine("Program is executed."); } Console.ReadLine(); } }

Exception occurs. Program is executed.

What is the use of the Immediate window in Visual Studio?

Execute commands while an application is running. Execute code statements that are valid in the context of a break point and inspect values.

Which of the following panels enables flexible positioning of child controls?

Grid

Which of the following properties allows a user to edit the text in the TextBox when this is set to true?

IsEnabled

At which line should you place the breakpoint to enter break mode just before the error is caused on the 5,000th cycle? static void Main(string[] args) { for (int i = 1; i < 10000; i++) //Line 1 { WriteLine($"Loop cycle {i}"); //Line 2 if (i == 5000) //Line 3 { WriteLine(args[999]); //Line 4 } } }

Line 2

Which of the following specifies the space to leave blank around the edges of controls?

Margin

Which of the following control events occurs continually as the mouse travels over the control?

MouseMove

public class MyClass { public class MyNestedClass { public int NestedClassField; } } Which line of code will you use to instantiate MyNestedClass from outside MyClass?

MyClass.MyNestedClass myObj = new MyClass.MyNestedClass();

Which of the following control program flow options runs to the end of the code block and resumes the break mode at the statement that follows?

Step Out

Consider the following code snippet showing object hierarchy: public class MyBaseClass { public MyBaseClass() { } public MyBaseClass(int i) { } } public class MyDerivedClass : MyBaseClass { public MyDerivedClass() { } public MyDerivedClass(int i) { } public MyDerivedClass(int i, int j) { } } If you instantiate MyDerivedClass using the MyDerivedClass myObj = new MyDerivedClass(); statement, then what will be the sequence of events that occur?

System.Object.Object() > MyBaseClass.MyBaseClass() > MyDerivedClass.MyDerivedClass()

Consider the following object hierarchy: public class MyBaseClass { public MyBaseClass() { } public MyBaseClass(int i) { } } public class MyDerivedClass : MyBaseClass { public MyDerivedClass() { } public MyDerivedClass(int i) { } public MyDerivedClass(int i, int j) { } } If you instantiate MyDerivedClass using the MyDerivedClass myObj = new MyDerivedClass(); statement, then what will be the order of events that will occur?

System.Object.Object() > MyBaseClass.MyBaseClass() > MyDerivedClass.MyDerivedClass()

Which of the following windows shows the way in which the program reached the current location?

The Call Stack window

Which of the following statements is true for the given lines of code? // Field used by property. private int myInt; // Property. public int MyIntProp { get { return myInt; } set { // Property set code. } }

The code external to the class cannot access the myInt field directly.

Which of the following statements is true in respect to the following code? interface a1 { void f1(); int f2(); } class a :a1 { void f1() {} int a1.f2() {} }

The definition of f1() in class a should be void a1.f1().

How many number of constructors can be defined in a class?

Unlimited number

What is the output? public static void Main(string[] args) { try { int x, y, z = 5; y = 0; x = z / y; Console.WriteLine("X"); } catch (ArithmeticException e) { int z = 5; int p = 10; int q = 2 * z - p; Console.WriteLine("Y"); Console.WriteLine(q); } Console.ReadLine(); }

Y 0

Which of the following is the correct way of implementing an interface addition by the class maths?

class maths : addition {}

Using the if-else double-selection statement: Perform the following tasks in the C# block: -Declare a static public and character function grade with the parameter as marks having the datatype as integer. -Declare a character variable grd. -Use an if condition to check if the marks obtained are greater than 89 and less than 101, then return the variable grd as A. -Use the else if condition to check if the marks obtained are greater than 59 and less than 90, then return the variable grd as B. -Use the else condition to return the variable grd as C, if marks obtained are less than 59. -Return the value of the variable grd using the return keyword

public static char grade(int marks) { char grd; if (marks > 89 && marks < 101) grd = 'A'; else if (marks > 59 && marks < 90) grd = 'B'; else grd = 'C'; return grd; }

Using the for loop - Part 2 -Assign the Boolean value true to the variable prime using the bool function. -Use the if condition to check whether (a==1) returns false or (a==2) returns true. -Initialize the for loop with the variable i whose initial value is 2 and execute the loop until the value of the variable i becomes less than or equal to a/2. -Use the if condition to check whether the variable a returns 0 when it is divided by i. -If the above condition is true and the remainder of a/i is zero, then set the value of the Boolean variable prime as false. -Set the variable prime as the returning value of the function

static public bool isPrime(int a) { if (a==1) return false; if (a==2) return true; bool prime = true; for(int i = 2; i<=a/2; i++){ if (a%i== 0){ prime = false; } } return prime; }

Using the switch statement -Check the condition of the entered value and redirect it to the specific case. -For cases '+', '-', '*', and '/', add, subtract, multiply, and divide the variables a and b, respectively, store the result in the variable result, and take the control outside the switch block using the break statement.

static public int calculator(int a, int b, char c) { int result = 0; switch (c){ case '+': result = a+b; break; case '-': result = a-b; break; case '*': result = a*b; break; case '/': result = a/b; break; } return result; }

Using the for loop - Part 1 -Declare and initialize the variable fact as 1. -Declare a for loop with an initial value of the variable i as 1, test condition as i <= a, and increment value of the variable i by 1. -Calculate the product of the variables i and fact, and store it in the variable fact. -Return the calculated factorial stored in the variable fact using the return keyword.

static public int factorial(int a) { int fact = 1; for (int i = 1; i<=a; i++) {fact *= i; } return fact; }

Understanding the do-while loop -Declare and initialize the value of the variables result and i as 1. -Declare a do-while loop to store the product of the variables i and result in the variable result within the loop. -Increment the value of the variable i by 1, each time the loop executes. -Add a test condition for the do-while loop in such a way that the loop executes till the value of the variable i becomes greater than a. -Return the calculated value of the variable result using the return keyword.

static public int factorial(int a) { int result = 1, i = 1; do {result *=i; i++; } while(i<=a); return result; }

Using the do-while loop Question 4 : In this lab, you will learn how to obtain the reverse of a number using the do-while loop.Complete the given code in the C# block by performing the following tasks: -Store the remainder of a % 10 in the variable i. -Add the value of the variable i to the product of the variable reverseNum and an integer 10. Store the resultant value in the variable reverseNum. -Divide the value stored in the variable a by 10 and store the value in the variable a. -Add a test condition for the do-while loop such that, if the value of the variable a is greater than 0, the loop gets executed.

static public int reverse(int a) { int reverseNum = 0, i=0; do { i = a % 10; reverseNum = reverseNum * 10 + i; a /= 10; } while(a > 0); return reverseNum; }

Obtaining the sum of even numbers using the for loop Question 3 : In this lab, you will learn how to calculate the sum of even numbers using the for loop.Complete the given code in the C# block by performing the following tasks:Declare an integer variable sum whose initial value is 0.Declare a for loop, initialize it with a variable i whose initial value is 1.Add a test condition using the if statement to check whether the variable i divided by two returns a zero remainder.Increment the value of the variable sum by i if the test condition is true.Return the calculated value of the variable sum, which is the sum of even numbers from 1 to n.

static public int sumEven(int n) { int sum = 0; for (int i = 1; i<=n; i++){ if(i%2 == 0) { sum += i; } } return sum; }

Using the while loop -Declare an integer variable count and set its initial value as 0. -Declare a while loop with the test condition as n > 0. -Increment the value of the variable count by adding the variable n to it. -Decrement the value of the variable n by 1. -Return the final value of the variable count as the returning value of the function after the control comes out of the iteration statement.

static public int sumNum(int n) { int count= 0; while (n>0) { count += n; n--; } return count; }

To convert the entered string str to upper case and set it as the returning value of the function upperCase(), use the following code:

static public string upperCase(string str) { return str.ToUpper(); }

Using the switch statement with the string Question 2 : In this lab, you will learn about using the switch statement with the string.Complete the given code in the C# block by performing the following tasks:Declare a switch block with the input case as a string str.Declare two cases as add and sum, and use Console.WriteLine() to display the sum of the two variables a and b.Use the break statement to take control outside the switch block after displaying the sum of the variables.Declare two cases as diff and sub, and use Console.WriteLine() to display the difference of the two variables a and b.Use the break statement to take control outside the switch block after displaying the difference of the variables.Declare a default case and use Console.WriteLine() to display the product of the two variables a and b.Use the break statement to take control outside the switch block after displaying the product of the variables.

static public void checkStr(string str, int a, int b) { switch(str){ case "add": case "sum": Console.WriteLine(a+b); break; case "diff": case "sub": Console.WriteLine(a-b); break; default: Console.WriteLine(a*b); break; } }

Complete the given code in the C# block by performing the following tasks: -Use Console.Write() to display the values of the variables a and b as 0 and 1, respectively. -Declare a while loop with the test condition as i < len. -Add the value of the two variables a and b, store it in the variable c, and display the value of the variable c. -Swap the value of the variable a with b and b with c. -Increment the value of the variable i by 1.

static public void fibb(int len) { int a = 0, b = 1, c = 0, i = 2; Console.Write("{0} {1}", a,b); while(i < len) { c = a + b; Console.Write(" {0}", c); a = b; b = c; i++; }}

Using the if-else statement within the while loop Question 5 : In this lab, you will learn how to use the if-else statement within the while loop to find an Armstrong number. Complete the given code in the C# block by performing the following tasks: -Execute the while loop till the test condition n > 0 is true. -Within the while loop, store the remainder of the expression n % 10 in the variable r. -Add the variable sum to the cube of the variable r and store it in the variable sum. -Divide the number n by 10 and store the result in the variable n. -After exiting from the loop, use the if statement to check whether the variable temp is equal to the variable sum. -Use Console.Write() to print True, if the test condition is true, and False if the test condition is false.

static public void isArmstrong(int n) { int r,sum = 0,temp; temp = n; while(n > 0) { r = n % 10; sum = sum + (r * r * r); n = n/10; } if(temp==sum) { Console.Write(true); } else { Console.Write(false); } }

Using access modifiers In this lab, you will learn the use of access modifiers.In the C# block, modify the class Math such that it inherits the base class Initial.

using System; namespace AccessModifiers { class Program { class Initial { protected int num1 = Convert.ToInt32(Console.ReadLine()); protected int num2 = Convert.ToInt32(Console.ReadLine()); } class Math : Initial { static void Main(string[] args) { Math obj = new Math(); Console.WriteLine(obj.num1 + obj.num2); } } } }

Understanding the variable scope Question 5 : In this lab, you will learn about the variable scope.Complete the given code in the C# block by performing the following tasks:Declare a public function Program with an integer parameter x.Store the value of the variable x in the public integer variable i.

using System; namespace CsharpPrograms { class Program { public int i; public Program(int x){ i = x; } static void Main(string[] args) { int x = Convert.ToInt32(Console.ReadLine()); Program obj = new Program(x); Console.WriteLine(obj.i); } } }

Understanding functions - Part 2 -Declare a static and non-returning value function using the void keyword with the name Add and two integer parameters as x and y. -Declare an integer variable sum and store the sum of the variables x and y in the variable sum. -Use Console.WriteLine() to display the value of the variable sum.

using System; namespace CsharpPrograms { class Program { static void Main(string[] args) { int x,y; x = Convert.ToInt32(Console.ReadLine()); y = Convert.ToInt32(Console.ReadLine()); Add(x,y); } static void Add(int x, int y){ int sum = x+y; Console.WriteLine(sum); } } }

Understanding functions - Part 1 -Declare a static public and integer function Average with five parameters as a, b, c, d, and e having the datatype as float. -Calculate the average of the five variables and save it in the integer variable avg. -Set the variable avg as the returning value of the function Average.

using System; namespace CsharpPrograms { class Program { public static int Average(float a, float b, float c, float d, float e){ int avg = (int) (a+b+c+d+e)/5; return avg; } static void Main(string[] args) { float v,w,x,y,z; int result=0; v = float.Parse(Console.ReadLine()); w = float.Parse(Console.ReadLine()); x = float.Parse(Console.ReadLine()); y = float.Parse(Console.ReadLine()); z = float.Parse(Console.ReadLine()); result = Average(v,w,x,y,z); Console.WriteLine(result); } } }

Understanding operator precedence Question 6 : In this lab, you will learn about operator precedence.In the C# block, perform an arithmetic operation to find the difference of the variables a and b, sum of the variables c and d, and then multiply the resultants and store it in an integer variable ans.

using System; namespace CsharpPrograms { class Progra { static void Main(string[] args) { int w,x,y,z,result=0; w = Convert.ToInt32(Console.ReadLine()); x = Convert.ToInt32(Console.ReadLine()); y = Convert.ToInt32(Console.ReadLine()); z = Convert.ToInt32(Console.ReadLine()); result= calculate(w,x,y,z); Console.WriteLine(result); } static public int calculate(int a, int b, int c, int d) { int ans; ans = (a-b)*(c+d); return ans; } } }

Complete the given code in the C# block by performing the following tasks: Declare a public static and integer function Add and two parameters a and b of type integer. Use the return keyword to set the sum of the two variables a and b as the returning value of the function. Declare a public static and integer function Add and three parameters a, b, and c of type integer. Use the return keyword to set the sum of the three variables a, b, and c as the returning value of the function.

using System; namespace CsharpPrograms { class Program { static public int Add(int a, int b) { return a+b; } static public int Add(int a, int b, int c=0) { return a+b+c; } static void Main(string[] args) { int x,y,z,result=0; x = Convert.ToInt32(Console.ReadLine()); y = Convert.ToInt32(Console.ReadLine()); z = Convert.ToInt32(Console.ReadLine()); if(z>0 || z<0) { result = Add(x,y,z); } else { result = Add(x,y); } Console.WriteLine(result); } } }

Using the nested for loop for printing a pattern In this lab, you will learn how to use the nested for loop to print a pattern.Complete the given code in the C# block by performing the following tasks: -Declare a character variable x and set its initial value as a and declare an integer variable i and set its initial value as 1. -Declare the for loop with an initial value of the variable i as 1, test condition as i < 5, and increment the value of the variable i by 1. -Declare the for loop with an initial value of the variable j as 1, test condition as j <= i, and increment the value of the variable j by 1. -Use Console.WriteLine() to display the value of the variable j. -After the loop ends, use Console.WriteLine() to display the value of the variable x and increment its value by 1 each time the loop executes.

using System; namespace CsharpPrograms { class Program { static void Main(string[] args) { char x='a'; int i=1; for(i=1; i<5; i++) { for(int j=1; j<=i; j++) { Console.Write(j); } Console.Write(" {0} ",x++); } } } }


Kaugnay na mga set ng pag-aaral

Lesson 3 Physical and Chemical Changes

View Set

(10) Anatomy- Respiratory System (Gas Exchange)

View Set

Chapter 13 How Cells Obtain Energy from Food

View Set

U.S. History Chapter 14 Section 2 The Movement Gains Ground

View Set

Pharmacology Made Easy 4.0 Infection

View Set

CH 7 - Business-Government Relations

View Set