Saltar al contenido
Examen 10 preguntas

Variables and Data Types in C#

Preguntas 10

Las respuestas correctas se muestran a los docentes que han iniciado sesión.

  1. Opción múltipleProgramming languagesc#local variablesdefault valuesuninitialized variables

    Value type default

    What is the default value of an uninitialized local variable of type int in C# when it is read after being declared but before being assigned?

    1. undefined

    2. -1

    3. 0

    4. null


    Think about how the compiler treats local variables: it refuses to let you read them unless they are definitely assigned, so consider what value a field of this type would get by default.
  2. Opción múltipleProgramming languagesc#type conversionnumeric typesimplicit conversion

    Implicit conversion

    Which of the following conversions is performed implicitly by the C# compiler without requiring an explicit cast?

    1. Assigning a string to an int variable

    2. Assigning a long value to an int variable

    3. Assigning an int value to a double variable

    4. Assigning a double value to an int variable


    Consider which conversion can never lose information, because the range and precision of the target type fully contain the source values.
  3. Opción múltipleProgramming languagesc#integer divisionarithmetic operators

    Integer division

    What is the result of the expression 7 / 2 when both operands are of type int in C#?

    1. 4

    2. 3.0

    3. 3.5

    4. 3


    Recall what happens to the fractional part when integer operands are divided in C#.
  4. Opción múltipleProgramming languagesc#type conversionConvert classformat exception

    Explicit conversion with Convert

    What does the expression Convert.ToInt32("42abc") do in C# when executed?

    1. Returns null

    2. Returns 0

    3. Returns 42

    4. Throws a FormatException


    Consider whether the string can be fully parsed as a valid integer, and what the Convert class does when parsing fails.
  5. Opción múltipleProgramming languagesc#var keywordtype inferencestatic typing

    var keyword

    Which statement about the var keyword in C# is correct?

    1. var is only allowed for reference types such as string

    2. var variables can be declared without any initial value

    3. var makes a variable dynamically typed, so its type can change at run time

    4. The compiler infers the static type of a var variable from its initializer at compile time


    Focus on when the type is determined: is it decided while the program runs, or while the code is compiled from the right-hand side of the declaration?
  6. Opción múltipleProgramming languagesc#data losstype conversionexplicit cast

    Explicit cast and data loss

    What is the value of int result = (int)3.99; in C#?

    1. 3.99

    2. Compile-time error

    3. 3

    4. 4


    An explicit cast from a floating-point type to an integer type truncates toward zero rather than rounding.
  7. Opción múltipleProgramming languagesc#type conversionparsingstring to int

    Parsing and value types

    Which C# code correctly converts the string "123" to an int and stores it in a variable named number, using a method that is designed for parsing text?

    1. int number = Convert.ToString("123");

    2. int number = (string)"123";

    3. int number = "123".ToInt();

    4. int number = int.Parse("123");


    Look for the static method on the int type whose purpose is to read a number from a string.
  8. Verdadero / falsoProgramming languagesc#value typesreference typesdata types

    Value types and reference types

    In C#, the int data type is a value type, which means a variable of type int stores its actual value directly rather than a reference to the value.

    1. True

    2. False


    Think about where the data itself is kept for simple numeric types such as int, compared with objects created with new.
  9. Verdadero / falsoc#data typesfloating-point precisiondouble vs float

    Floating-point precision

    In C#, a variable declared as double can store a value with more precision than a variable declared as float.

    1. True

    2. False


    Compare the number of bits each floating-point type uses to represent a value.
  10. Verdadero / falsoc#data typestype castingexplicit conversion

    Explicit conversion from double to int

    In C#, assigning a double value to an int variable without an explicit cast causes a compile-time error.

    1. True

    2. False


    Consider whether the conversion from double to int can lose the fractional part, and what the compiler requires in that case.

Acerca de

Covers declaring variables, common data types, and simple type conversion in C# programs.
Materias
c#
Público
Escuela secundaria · Grados 10–12
Dificultad
6 de 10
Idioma
Inglés
Quién puede verlo
Todo el mundo
Completado por IA
Lo marcado con un destello lo añadió la IA.
Escrito por IA
Claude Haiku 5.5
Creado

Comentarios

  1. Todavía no hay comentarios.
© Todos los derechos reservados